Comment

Comment by generateui

> 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.

parent generateui 14 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton