Design Patterns - Flyweight Pattern

Flyweight Pattern - Flyweight Pattern

My understanding: The same object is used multiple times, only the external state is different. In the flyweight pattern factory, the same object and different external states can be assigned to different references.

Flyweight Pattern : Use sharing technology to effectively support the reuse of a large number of fine-grained objects. The system only uses a small number of objects, and these objects are very similar, and the state changes are small, which can realize multiple reuse of objects. Since the Flyweight pattern requires that the objects that can be shared must be fine-grained objects, it is also called the lightweight pattern, which is an object structure pattern.

      ●  Abstract flyweight class: usually an interface or abstract class, in which the public methods of the concrete flyweight class are declared. These methods can provide the outside world with the internal data (internal state) of the flyweight object, and can also Use these methods to set external data (external state).

      ●  Concrete flyweight class: It implements abstract flyweight class, and its instance is called flyweight object; it provides storage space for internal state in the concrete flyweight class. Usually, we can design specific flyweight classes in combination with the singleton pattern, and provide a unique flyweight object for each specific flyweight class.

      Non-shared concrete flyweight  classes: not all subclasses of abstract flyweight classes need to be shared, and subclasses that cannot be shared can be designed as non-shared concrete flyweight classes; when a non-shared concrete flyweight class is required Objects can be created directly by instantiation .

      Flyweight  factory class: Flyweight factory class is used to create and manage flyweight objects. It is programmed for abstract flyweight classes and stores various types of specific flyweight objects in a flyweight pool. The flyweight pool is generally designed as A collection that stores "key-value pairs" (or other types of collections) can be designed in conjunction with the factory pattern; when a user requests a specific flyweight object, the flyweight factory provides a store that has been created in the flyweight pool instance or create a new instance (if it does not exist), return the newly created instance and store it in the flyweight pool.


abstract flyweight class

// abstract flyweight class
public abstract class abstractFile {
    public abstract String getType();

    public void save(Detail lt){
        System.out.println("file type:"+this.getType()+"file size"+lt.getSize()+"file location"+lt.getPath());
    }

}

Concrete flyweight class

public class ConcreteImage extends abstractFile {
    @Override
    public String getType() {
        return "Image";
    }
}
public class ConcreteAnimation extends abstractFile {
    @Override
    public String getType() {
        return "Animation";
    }
}
public class ConcreteVideo extends abstractFile {
    @Override
    public String getType() {
        return "Video";
    }
}
Flyweight Factory

public class FileFactory {
    private static FileFactory factory = new FileFactory();
    private static Hashtable ht;

    private FileFactory() {
        ht = new Hashtable();
        abstractFile Animation,Image,Video;
        ht.put("A",new ConcreteAnimation());
        ht.put("I",new ConcreteImage());
        ht.put("V",new ConcreteVideo());
    }

    public static FileFactory getFactory(){
        return factory;
    }

    public static abstractFile getFileinFactory(String type){
        return (abstractFile) ht.get(type);
    }

}

client

public class Client {
    public static void main(String[] args) {
        abstractFile a1,a2,a3,a4;
        FileFactory.getFactory();

        a1 = FileFactory.getFileinFactory("A");
        a2 = FileFactory.getFileinFactory("I");
        a3 = FileFactory.getFileinFactory("V");
        a4 = FileFactory.getFileinFactory("A");

        a1.save(new Detail("C:/Windows/System32",128));
        a2.save(new Detail("C:/Windows/System64",256));
        a3.save(new Detail("C:/Windows/System128",512));
        a4.save(new Detail("C:/Windows/System256",1024));

        System.out.println(a1 == a4);

    }
}






Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326852204&siteId=291194637