method-chaining

Builder pattern with nested objects

做~自己de王妃 提交于 2019-11-30 07:07:12
Hi I'm stuck with a problem. I want to implement the builder pattern to make creating my objects easier. The problem I face has to do with nested object. The object I would like to create has a list of other objects in it, and I don't really have an idea on how to tackle it. I want to be able to do the following (Simpler objects for example): Receipt RestaurantReceipt = new ReceiptBuilder() .withDate("value") .withName("value") .AddItem("value") .WithIngredients("value") .WithType("value") .AddItem("value") .WithIngredients("value") .WithType("value") .build(); Or something like: Receipt

Basic method chaining

吃可爱长大的小学妹 提交于 2019-11-29 22:20:26
I found this method chaining in python , but even with it I couldn't understand method chaining in Python. Here the goals are two: solve the coding problem and understand method chaining (given that I am still not 100% confident with callables). Down to the problem definition. I want a class that has two methods: one sets a parameter of the object = 'line' and the other overwrites to 'bar'. This is what I got so far: class foo(): def __init__(self, kind=None): self.kind = kind def __call__(self, kind=None): return foo(kind=kind) def my_print(self): print (self.kind) def line(self): return self

Is operator-> “chained” for pointers? [duplicate]

狂风中的少年 提交于 2019-11-29 14:57:02
问题 Possible Duplicate: Overloading operator -> Hi, I've seen that operator->() is chained (re-applied) after it is evaluated, for example: struct Bar { Bar() : m_str("Hello world!") {} const string* operator->() const { return &m_str; } string m_str; }; struct Foo { const Bar& operator->() const { return m_bar; } Bar m_bar; }; int main() { Foo f; cout << f->c_str() << endl; return 0; } works pretty fine, which requires three operator->() to be evaluated - Foo::operator->() , Bar::operator->()

Method Chaining: How to use getThis() trick in case of multi level inheritance

荒凉一梦 提交于 2019-11-29 02:29:15
My question is in context of Method chaining + inheritance don’t play well together? . But unfortunately all examples/answers of method chaining uses single level of inheritance. My usecase involves multi level of inheritance for e.g abstract class PetBuilder{...} class DogBuilder extends PetBuilder{..} class DogType1Builder extends DogBuilder {...} To construct a Dog Object,i will be using either DogBuilder or DogType1Builder how to use getThis trick for the above use case? I want to use builder pattern for constructing a complicated Dog object(Dog Object Model)" . DogType1 will have some

Method chaining + inheritance don't play well together?

我是研究僧i 提交于 2019-11-28 20:43:03
Consider: // member data omitted for brevity // assume that "setAngle" needs to be implemented separately // in Label and Image, and that Button does need to inherit // Label, rather than, say, contain one (etc) struct Widget { Widget& move(Point newPos) { pos = newPos; return *this; } }; struct Label : Widget { Label& setText(string const& newText) { text = newText; return *this; } Label& setAngle(double newAngle) { angle = newAngle; return *this; } }; struct Button : Label { Button& setAngle(double newAngle) { backgroundImage.setAngle(newAngle); Label::setAngle(newAngle); return *this; } };

Chaining methods with javascript

旧街凉风 提交于 2019-11-28 14:20:34
I'm trying to create chaining with the javascript methods similar to what we have with jquery. Please let me know how to implement chaining with javascript. var controller = { currentUser: '', fnFormatUserName: function(user) { this.currentUser = user; return this.currentUser.toUpperCase(); }, fnCreateUserId: function() { return this.currentUser + Math.random(); } } var output = controller.fnFormatUserName('Manju').fnCreateUserId(); As I already explained, since you are returning a string from fnFormatUserName you cannot use it for chaining. To enable chaining, you need to return the object

How to build multi oop functions in PHP5

对着背影说爱祢 提交于 2019-11-28 11:45:24
I have a question about OOP in PHP5. I have seen more and more code written like this: $object->function()->first(array('str','str','str'))->second(array(1,2,3,4,5)); But I don't know how to create this method. I hope somebody can help me here, :0) thanks a lot. The key to chaining methods like that within your own classes is to return an object (almost always $this ), which then gets used as the object for the next method call. Like so: class example { public function a_function() { return $this; } public function first($some_array) { // do some stuff with $some_array, then... return $this; }

Type safe method chaining that doesn't allow repeats of operations

北城余情 提交于 2019-11-28 11:25:08
I want to implement method chaining like in those questions: Best practice to implement Scala trait which supports method chaining ; Scala DSL: method chaining with parameterless methods However, I want that once a "property" has been used, it cannot be used anymore. For example lets assume that I have a class "Myclass" for which I want to allow the use of definition "foo" and definition "bar" at most once and I don't care about the final return type. Thus: val c = new Myclass c foo //ok ! c foo bar // ok! c foo foo // refuse to compile c foo bar foo //refuse to compile I'm struggling with

Method Chaining vs |> Pipe Operator

好久不见. 提交于 2019-11-28 09:41:26
So I have the following code: // Learn more about F# at http://fsharp.net open System open System.Linq open Microsoft.FSharp.Collections let a = [1; 2; 3; 4; 54; 9] let c = a |> List.map(fun(x) -> x*3) |> List.filter(fun(x) -> x > 10) let d = a.Select(fun(x) -> x*3).Where(fun(x) -> x > 10) for i in c do Console.WriteLine(i) for i in d do Console.WriteLine(i) Both seem to do the same thing, but most F# examples I see use the |> pipe operator, while I'm more used to method chaining (a.l.a. C# Linq). The latter is also somewhat shorter, albeit somewhat more crunched together. For now i'm using

PHP OOP: Method Chaining

放肆的年华 提交于 2019-11-28 08:15:14
问题 I have the following code, <?php class Templater { static $params = array(); public static function assign($name, $value) { self::$params[] = array($name => $value); } public static function draw() { self::$params; } } $test = Templater::assign('key', 'value'); $test = Templater::draw(); print_r($test); How can I alter this script so I could use this? $test = Templater::assign('key', 'value')->assign('key2', 'value2')->draw(); print_r($test); 回答1: You cannot use Method Chaining with static