5 ms·
I really don't understand what advantage they provide over regular static methods...
by thurn 14y ago
I really don't understand what advantage they provide over regular static methods...
- afsina 14y agoI think they are there for reducing the need for dependency injection frameworks. Some discussion https://plus.google.com/u/0/110377859584351043951/posts/1FsB87X3fFp https://plus.google.com/u/0/110377859584351043951/posts/1FsB... http://code.google.com/p/dart/issues/detail?id=76 http://code.google.com/p/dart/issues/detail?id=76
- nl 14y agoWell.. DI Frameworks were originally created to provide an alternative to Factories.
- generateui 14y agoThere does not need to be any knowledge of _any_ class. A factory method _still_ needs a class instance, you don't need them with factory constructors. A classical, say Java or C# way will still need coupling to such a class. In dart, there is real decoupling going on.
- masklinn 14y ago> A factory method _still_ needs a class instance Uh no it does not, it just needs a class. Unless what you mean by "a class instance" is "a class object". A factory method is a classmethod, not an instancemethod. A factory method and a "factory constructor" need exactly the same thing: the class object. > In dart, there is real decoupling going on. In dart, there's mostly a bloody mess of 4 different constructors when all you need is a constructor and an initializer, as done in e.g. Ruby or Python. And these can be "regular" methods (respectively on the class and on the instance). Conversely, you can also have an "overridden" constructor as in Javascript: an instance is built, the constructor function is called using the instance as its context, and if the constructor returns something that something is returned by `new` in stead of the original object.
- generateui 14y ago> A factory method is a classmethod, not an instancemethod. This completely depends on your implementation. It's possible to implement it as instance method, or static method. > Uh no it does not, it just needs a class. Whether it be a static factory method or an instance method, fact is you need a reference to an implementation (class). In dart, you can do List someListInstance = new List(); where List is an interface. There is none class needed in your consuming code, none. In C# or Java, this is impossible: You need _some_ class (whether it be a static method on it, or an instance method) to get a new instance of the interface. Named constructors felt very liberating when working with Dart. new List.from([instance1, instance1]); delivers a lot of clarity, while offering the promised decoupling by the GoF: > Define an interface for creating an object, but let the classes which implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.
- nl 14y agoThat looks like the named-default-class-for-an-interface feature, not something specific to the factory pattern. (I don't know Dart, though)
- masklinn 14y ago> It's possible to implement it as instance method It makes no sense, if you're doing that you've switched to full-blown factories (abstracts or builders), you're not using factory methods anymore. > Whether it be a static factory method or an instance method, fact is you need a reference to an implementation (class). In dart, you can do > List someListInstance = new List(); > where List is an interface. There is none class needed in your consuming code, none. That's got nothing to do with factories, or interfaces. > You need _some_ class (whether it be a static method on it, or an instance method) to get a new instance of the interface. You always need a class, even in Dart there has to be a default List implementation of some sort which can be called (and has to be specified on the interface, it's not like you can even provide your own default implementation of a third party's interface). As to the calling code needing to know about that one concrete implementation: abstract class Foo { public static Foo foo() { return new FooImpl(); } public abstract void printFoo(); } class FooImpl extends Foo { public void printFoo() { System.out.println("I am FooImpl"); } } class Main { public static void main(String[] args) { Foo foo = Foo.foo(); foo.printFoo(); } } Oh look at that, the calling code does not know anything about FooImpl. > Named constructors felt very liberating when working with Dart. Yeah, that's so much more liberating than: List.from([instance1, instance2]); Wait, no it's not.
- numbsafari 14y agoIt comes down to the fact that interfaces aren't classes. To whit: let's say you want to have an interface for associative arrays. Let's call it Map. Now let's say you want to provide a static method to create an instance of the default Map implementation (call it, say, HashMap). So you'd think about doing something like this: interface Map { ... static defaultImpl() { .... } } Except, interfaces can't have implementation in them. That would imply multiple inheritance (which languages like Dart and Java don't support). Okay, so one workaround: use an abstract class: abstract class Map { ... static defaultImpl() { .... } } The only problem is that if you define Map as an abstract class instead of an interface, that means that all implementations of Map must extend Map. Given single inheritance, this means anything providing a Map-style interface can and must only extend from the base Map class. So... Factory classes are there because of how Java treats interfaces and single inheritance.