Factory keyword usage in Dart

factory introduction

In Dart, the factory keyword is used to define a factory constructor. It differs from normal constructors in the following ways:

  1. A call to the factory constructor can return instances of subtypes or other types. Ordinary constructors always return an instance of their enclosing type.
  2. The factory constructor can have a return value. The return value of a normal constructor is always an instance of its containing type.
  3. The factory constructor does not need to initialize all fields of the containing type. Ordinary constructors need to ensure that all non-null fields have values.

Benefits of using factories

  1. Other types of instances can be returned according to the parameters passed in, increasing flexibility.
  2. It is not necessary to initialize all the fields containing the type, and the returned instance attributes can be determined to increase flexibility.
  3. Can have a return value, not limited to returning an instance of the contained type, increasing flexibility.
    In general, when your constructor does not initialize instances exactly as defined by the containing type, or may return instances of other types, you should define it as a factory constructor.

example

Here is an example to illustrate the usage of the factory constructor:

class Image {
    
    
  final String name;
  Image(this.name);
}

class ImageCache {
    
    
  static Map<String, Image> _cache = {
    
    };

  factory ImageCache.fromName(String name) {
    
    
    if (_cache.containsKey(name)) {
    
    
      return _cache[name];
    } else {
    
    
      final image = Image(name);
      _cache[name] = image;
      return image;
    }
  }
}

Here we have:

  • The Image class represents an image with a name field.
  • The ImageCache class represents the image cache, and there is a _cache field to store all the images.
  • ImageCache has a factory constructor fromName, which is used to create ImageCache from the picture name.
    Then the usage of this constructor is as follows:
    dart
    var image1 = ImageCache.fromName('image1');
    var image2 = ImageCache.fromName('image2');
    var image3 = ImageCache.fromName('image1');
    when called for the first time , an instance of Image('image1') will be created, stored in the cache, and returned.
    When called for the second time, an instance of Image('image2') will be created, stored in the cache, and returned.
    But when called for the third time, the instance of the first Image('image1') will be returned directly from the cache.
    So there are several advantages to using the factory constructor here:
  1. Depending on the name, an instance of Image or ImageCache can be returned to increase flexibility.
  2. It is not necessary to create a new Image instance every time it is called, and the instance in the cache can be reused to optimize performance.
  3. There is an instance of the return value Image or ImageCache, not just an instance of ImageCache.
    This is a typical example of using the factory constructor for instance reuse and flexible return values.
    image.png

Guess you like

Origin blog.csdn.net/yikezhuixun/article/details/130529229