top of page

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

  1. A factory constructor is for creating a singleton.

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

  1. factory pattern

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

コメント


category

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page