About factory constructor in Dart [duplicate]
Asked Answered
L

1

12

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#?

Leyes answered 12/9, 2014 at 0:18 Comment(1)
Please do not ask the same question twice. If you had't edited the other question to have the same content as this one it would have made more sense but now they are completely indentical.Catalan
C
14

The factory PATTERN is the same. It's a general pattern and is not language specific.

Dart provides factory constructors to support the factory pattern. The factory constructor is able to return values (objects). In your first example you check if there is an instance for the key you return it.

In the second example you don't check the key of the map and you don't return the instance. That's why the two instances are not identical.

Regards, Robert

Chalcidice answered 12/9, 2014 at 1:44 Comment(3)
Thank you, Robert. your comments helped me a lot, especially 'RETURN' means more than I have known it does. Anyway, all materials I found about factory pattern explains it in terms of Java or C# or Lisp. Could anybody explain it in terms of Dart or JS for those who don't understand Java, C#, or Lisp?Leyes
The more I learn Dart, the more Java seems prerequisite of it as a few key concepts of Dart are explained in Java prospect.Leyes
The factory patter generally applies to all languages. Dart only has a special language concept (factory constructers) to make it easier. In Java you just have a singleton factory that generates objects for you.Chalcidice

© 2022 - 2024 — McMap. All rights reserved.