[Fluter/dart]Just adding the factory keyword does not generate singleton
Overview
In dart, you can add the factory keyword to the constructor.
I made the following mistake.
A factory constructor is for creating a singleton.
If you add the factory keyword to the constructor, it will automatically generate a singleton
I will explain this.
What is factory constructor
First, about Misunderstanding 1, there are mainly two situations where the factory constructor is used:
factory pattern
singleton pattern
factory pattern
Consider a parent class and many child classes that derive from it. If the user of class instance can be designed generally with the parent class as the target, the number of cases will be reduced and new types of child classes can be easily added.
Such a design is the factory pattern.
For example, suppose you have a parent class Figure and child classes Rectangle, Circle, and so on. A drawing application that uses Figure instances should be designed with the Figure class as the target without distinguishing between individual Rectangles, Circles, and so on.
class Figure{
double width;
double height;
・・・
}
class Rectangle extends Figure{
}
class Circle extends Figure{
}
//A drawing application
Figure create(int kind){
}
void move(Figure figure){
}
void delete(Figure figure){
}
The problem here is when creating a new figure (create() above).
As shown below, it is nonsense to divide cases according to the argument "kind". We would like the drawing application not to know the details of child classes such as Rectangle.
(For example, imagine a modification when a new shape is added. Even the application side needs to be modified.)
Figure create(int kind){
switch(kind){
case RECT:
return Rectangle();
・・・
}
}
The factory constructor addresses this issue. Allow the Figure class constructor to return an instance of the child class.
Figure.create(int kind){ //Note: Cannot compile as is
switch(kind){
case RECT:
return Rectangle();
・・・
}
}
The application side just calls the constructor of the Figure class.
Figure create(int kind){
return Figure.create(kind);
}
Singleton pattern
Another use case for the factory constructor is the Singleton pattern.
It is used when you only need one instance of a class in your app.
For example, a class that collects the properties of something.
In this case, the constructor caches the instance on the first call and returns the cached instance on subsequent calls.
Propeties props;
class Properties{
Properties.create(){ //Note: Cannot compile as is
if (props == null){
props = Properties();
}
return props;
}
//constoructor
Properties(){
}
}
Factory keyword
The factory constructor is used to implement the factory pattern and singleton pattern as described above.
公式 によれば、dartにおいてfactoryキーワードをつけると
必ずしも新しいインスタンスを返さなくても良い
ことになります。
According to the official page, if you add the factory keyword in dart
It is not necessary to return a new instance
factory Figure.create(int kind){
switch(kind){
case RECT:
return Rectangle();
・・・
}
}
In practice, as mentioned above,
return an instance of the child class
return the cached instance
And so on.
Please note that it's just "you can do XX", and it doesn't automatically do the above when you add the factory keyword.
Lastly
When I first learned about the factory constructor, the use case was the singleton pattern, so I probably misunderstood what I wrote at the beginning.
Recent Posts
See AllWhat want to do I want to create an input form using TextField. For example, if the input content is a monetary amount, I would like to...
What want to do There is a variable that remain unchanged once the initial value is determined. However, it cannot be determined yet when...
What want to do As the title suggests. Place two widgets in one line on the screen One in the center of the screen and the other on the...
コメント