Design Pattern: Flyweight Pattern

Flyweight: Use sharing techniques to efficiently support a large number of fine-grained objects.


Flyweight class, which is the superclass or interface of all concrete flyweight classes, through which Flyweight can accept and act on external states.

abstract class Flyweight{
    public abstract void Operation(int extrinsicstate);
}

ConcreteFlyweight inherits the Flyweight superclass or implements the Flyweight interface, and adds storage space for the internal state

class ConcreteFlyweight implements Flyweight{
    public void Operation(int extrinsicstate){
        System.out.println("具体Flyweight:"+extrinsicstate);
    }
}

UnsharedConcreateFlyweight refers to those Flyweight subclasses that do not need to be shared, because the Flyweight interface sharing is possible, but it does not enforce sharing.

class UnsharedConcreateFlyweight implements Flyweight{
    public void Operation(int extrinsicstate){
        System.out.println("Unshared concrete Flyweight:"+extrinsicstate);
    }
}

FlyweightFactory, a flyweight factory, is used to create and manage Flyweight objects. It is mainly used to ensure reasonable sharing of Flyweight. When a user requests a Flyweight, the FlyweightFactory object provides a created instance or creates one (if it does not exist) ).

class FlyweightFactory{
    private Hashtable Flyweights = new Hashtable();

    public FlyweightFactory(){
        //When initializing the factory, first generate three instances
        Flyweights.add("X", new ConcreteFlyweight());
        Flyweights.add("Y", new ConcreteFlyweight());
        Flyweights.add("Z", new ConcreteFlyweight());
    }

    public Flyweight getFlyweight(String key){
        //According to the client request, get the generated instance
        return ((Flyweight)flyweights[key]);
    }
}

client code

//code external state
int extrinsicstate = 22;
FlyweightFactory f = new FlyweightFactory();

Flyweight fx = f.getFlyweight("X");
fx.Operation(--extrinsicstate);

Flyweight fy = f.getFlyweight("Y");
fx.Operation(--extrinsicstate);

Flyweight fz = f.getFlyweight("Z");
fx.Operation(--extrinsicstate);

Flyweight uf = new UnsharedConcreateFlyweight();
uf.Operation(--extrinsicstate);

The result says:

Concrete Flyweight: 21
Concrete Flyweight: 20
Concrete Flyweight: 19

Unshared specific Flyweight: 18

Internal state and external state

      The shared part that is inside the Flyweight state and does not change with the environment can be called the internal state of the Flyweight object, and the state that changes with the environment and cannot be shared is the external state. In fact, the Flyweight pattern The overhead of a large number of very similar classes can be avoided. In program design, it is sometimes necessary to produce a large number of fine-grained class instances to represent data. If these instances can be found to be basically the same except for a few parameters, it can sometimes be greatly affected. Reduce the number of classes that need to be instantiated. If you can move those parameters out of the class instance and pass them in when the method is called, you can drastically reduce the number of individual instances through sharing.

      That is to say, the state required for the execution of Flyweight mode Flyweight is internal or external. The internal state is stored in the ConcreteFlyweight object, and the external object should consider the client object storage or calculation. When calling the Flyweight object's When operating, pass that state to it.

Flyweight Pattern Application

      If an application uses a large number of objects, and a large number of these objects cause a lot of storage overhead, it should be considered. Also, most of the state of the object can be external state. If the external state of the object is deleted, it can be used When relatively few shared objects replace many organizational objects, flyweight can be considered.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642045&siteId=291194637