HaxeFoundation / haxe.org-comments

Repository to collect comments of our haxe.org websites
2 stars 2 forks source link

[code.haxe.org] Design patterns - Method chaining / Fluent interface #92

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Method chaining / Fluent interface - Design patterns - Haxe programming language cookbook

This is an example of the Method chaining design pattern in Haxe.

https://code.haxe.org/category/design-patterns/method-chaining-fluent-interface.html

RblSb commented 1 year ago

There is no info in this article how to make method chaining if you don't care about inheritance. Here is a example, that can be useful for beginners:

// new Simon().sleep().talk().work().talk();
class Simon {
    public var energy = 0;

    public function new() {}

    public function sleep():Simon {
        energy = 2;
        return this;
    }

    public function talk():Simon {
        trace('My energy is $energy');
        return this;
    }

    public function work():Simon {
        energy--;
        return this;
    }
}