Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart factory constructor - how is it different to “const” constructor

In Dart, factory constructors needs more logic from coders, but not so different from const ones except they permit 'Non final' instance variables.

What are their merits over const constructors?

Thank you all.

Edited

Below is about a usage of factory constructor from Seth Ladd's blog ' Dart - Trying to understand the value of 'factory' constructor'.

class Symbol {
  final String name;
  static Map<String, Symbol> _cache = new Map<String, Symbol>();

  factory Symbol(String name) {
    if (_cache.containsKey(name)) {
      return _cache[name];
    } else {
      final symbol = new Symbol._internal(name);
      _cache[name] = symbol;
      return symbol;
    }
  }

  Symbol._internal(this.name);
}


main() {
  var x = new Symbol('X');
  var alsoX = new Symbol('X');

  print(identical(x, alsoX));  // true
}

IMHO, with general constructor, the same effect can be achieved with a subtle difference, but quite simpler.

class Symbol {
  static final Map<String, Symbol> cache = {};
  final String name;

  Symbol(name) {
    cache[name] = new Symbol._internal();
  }

  Symbol._internal();
}

main(){
var a = new Symbol('something');
var b = new Symbol('something');

print(identical(a, b)); // false!
print(Symbol.cache); //{something: Instance of 'Symbol'}
}

As shown above, though the two instances, a & b, are different objects, the effect is all the same as shown in 'print(Symbol.cache); //{something: Instance of 'Symbol'}' as a map object permit only one of same strings as its key.

So, my question was what are peculiar merits of factory constructor(or factory pattern) over general/const constructors? Because the above sample code alone shows no merit of factory constructor.

Could anyone explain what is so called 'Factory Pattern' in Dart language rather than Java/C#?

like image 630
Letsgo Avatar asked Sep 10 '14 02:09

Letsgo


People also ask

What is the difference between factory and constructor?

A constructor is concrete in that it creates objects as instances of a single class, and by a specified process (class instantiation), while a factory can create objects by instantiating various classes, or by using other allocation schemes such as an object pool.

What is factory constructor in Dart?

A factory constructor is a constructor that can be used when you don't necessarily want a constructor to create a new instance of your class. This might be useful if you hold instances of your class in memory and don't want to create a new one each time (or if the operation of creating an instance is costly).

What is the advantage of factory constructor flutter?

Since a factory constructor does not directly create a new instance, it cannot use a constructor initializer list. A normal constructor always returns a new instance of the class. A factory constructor is permitted to return an existing instance, an instance of a derived class, or null .

What are constant constructors in flutter?

A const constructor is an optimization! The compiler makes the object immutable, allocating the same portion of memory for all Text('Hi!') objects.


1 Answers

A factory constructor and a const constructor fulfill entirely different purposes. A const constructor allows to have an instance of a custom class in a compile time constant expression. See https://stackoverflow.com/a/21746692/217408 for more details about the const constructor.

A factory constructor and a constant method that returns a new instance of a class are more similar. The difference is, that a factory constructor is called with new like a normal constructor and has some limitations a constant method doesn't have.

The main difference between a normal constructor and a factory constructor is, that you can influence if actually a new instance is created and of what concrete type it is.

In https://www.dartlang.org/dart-tips/dart-tips-ep-11.html a cache is mentioned as a popular example. A factory constructor can check if it has a prepared reusable instance in an internal cache and return this instance or otherwise create a new one.

Another example is the singleton pattern. See https://stackoverflow.com/a/12649574/217408 for more details.

Another example is the factory pattern. You can for example have an abstract class A (which can't be instantiated) with a factory constructor that returns an instance of a concrete subclass of A depending for example on the arguments passed to the factory constructor.

Here is a similar question Dart - Trying to understand the value of 'factory' constructor

like image 161
Günter Zöchbauer Avatar answered Oct 19 '22 12:10

Günter Zöchbauer