There are multiple concepts going on so lets go through the example:
factory Dog()
{
return dog;
}
This defines a factory constructor. Factory constructors are much like normal static methods but must always return an object which are compatible with the type of which the factory constructor is part of. In our example the factory constructor must return a Dog
object.
It is not a constructor in that sense that we already have created a object when we enter this method. Again, it can be compared to static Dog Dog()
but is allowed to override the default constructor. So we must create the object manually and return it.
This allows us to control the behavior of when objects are created and in our case allows us to have a singleton pattern so when somebody are trying to create an instance of Dog
they will always get the same object.
Dog._internal();
This is called a named constructor. Constructors in Dart can have given names which makes it easier to distinguish different constructors. But also because Dart does not allows us to override methods with different number of parameters.
Also, because of the name starting with _
it means this constructor is marked private and cannot be used outside of the library it is part of. So if your code is part of a library, the code importing your library are not allowed to call your private constructor.
This is again a nifty trick to allow us to make a singleton since we don't want other than our own code to create a new instance (which are the only instance we want to create).
static final Dog dog = Dog._internal();
This is essential creating the singleton. The reason for this is that static
variables in Dart are lazy-evaluated so the value dog
does not really have any value before it is called. After first call, the value are "cached" so Dog._internal(7)
are only called once as long our application (or more specific, our isoleate instance) are running.
I would properly call the variable _instance
or _dog
so it is also private.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…