Design Pattern Flyweight Pattern Notes

illustrate

Record the writing method of learning design pattern-flyweight pattern. The JDK version used is version 1.8.

Flyweight(flyweight)

Intent : Use sharing technology to efficiently support large numbers of fine-grained objects.
Structure :
Insert image description here

in:

  • Flyweight describes an interface through which Flyweight can accept and act on external states.
  • ConcreteFlyweight implements the Flyweight interface and adds storage space for internal state (if any). ConcreteFlyweight objects must be shareable. The state it stores must be internal, i.e. it must be independent of the ConcreteFlyweight object's scene.
  • Not all Flyweight subclasses need to be shared. The Flyweight interface makes sharing possible, but it does not force sharing. At some level in the Flyweight object structure, UnsharedConcreteFlyweight objects usually have ConcreteFlyweight objects as child nodes.
  • FlyweightFactory creates and manages Flyweight objects; ensuring reasonable sharing of Flyweights, when the user requests a Flyweight, the FlyweightFactory object provides a created instance or creates an instance if it does not exist.
  • Client maintains a reference to Flyweight: calculates or stores the external state of one or more Flyweights.

applicability:

  • An application uses a large number of objects.
  • This is entirely due to the use of a large number of objects, resulting in a large storage overhead.
  • Most of an object's state can be made external.
  • If you remove the external state of objects, you can replace many groups of objects with relatively few shared objects.
  • The application does not rely on object identity. Because Flyweight objects can be shared, identity tests will return true for conceptually distinct objects.

scenes to be used:

  • A system has a large number of identical or similar objects, causing a large amount of memory consumption.
  • Most of the state of an object can be externalized, and these external states can be passed into the object.
  • When using the flyweight mode, you need to maintain a flyweight pool to store flyweight objects, and this requires a certain amount of system resources. Therefore, it is worthwhile to use the flyweight mode only when you need to reuse the flyweight object multiple times.

Table of contents

Insert image description here

Flyweight pattern example class diagram

Insert image description here
Insert image description here
Use this UML class diagram to implement the flyweight pattern example.

abstract graphics class

package com.example.deesign_patterns.flyweight;

//抽象享元角色
public abstract class AbstractBox {
    
    

    //获取图形的方法
    public abstract String getShape();

    //显示图形及颜色
    public void display(String color){
    
    
        System.out.println("方块形状:"+getShape()+",方块颜色:"+color);
    }
}

IGraphics

package com.example.deesign_patterns.flyweight;

//I图形类(具体享元角色)
public class IBox extends AbstractBox{
    
    

    @Override
    public String getShape() {
    
    
        return "I";
    }
}

LGraphics class

package com.example.deesign_patterns.flyweight;

//L图形类(具体享元角色)
public class LBox extends AbstractBox{
    
    

    @Override
    public String getShape() {
    
    
        return "L";
    }
}

OGraphics class

package com.example.deesign_patterns.flyweight;

//O图形类(具体享元角色)
public class OBox extends AbstractBox{
    
    

    @Override
    public String getShape() {
    
    
        return "O";
    }
}

Factory class

package com.example.deesign_patterns.flyweight;

import java.util.HashMap;

//工厂类,将该类设计为单例
public class BoxFactory {
    
    

    private HashMap<String,AbstractBox> map;

    //使用单例模式-饿汉式静态成员变量方式
    private static BoxFactory factory=new BoxFactory();

    //在构造方法中进行初始化操作
    public BoxFactory() {
    
    
        map=new HashMap<String,AbstractBox>();
        map.put("I",new IBox());
        map.put("L",new LBox());
        map.put("O",new OBox());
    }

    //提供一个方法获取该工厂类对象
    public static BoxFactory getInstance(){
    
    
        return factory;
    }

    //根据名称获取图形对象
    public AbstractBox getShape(String name){
    
    
        return map.get(name);
    }
}

Test class

package com.example.deesign_patterns.flyweight;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //获取I图形对象
        AbstractBox box1 = BoxFactory.getInstance().getShape("I");
        box1.display("灰色");
        //获取L图形对象
        AbstractBox box2 = BoxFactory.getInstance().getShape("L");
        box2.display("绿色");
        //获取O图形对象
        AbstractBox box3 = BoxFactory.getInstance().getShape("O");
        box3.display("灰色");
        //获取O图形对象
        AbstractBox box4 = BoxFactory.getInstance().getShape("O");
        box4.display("红色");
        System.out.println("两次获取到的O图形对象是否是同一个对象:"+(box3==box4));
    }
}

Insert image description here

benefit:

  • Greatly reduce the number of similar or identical objects in memory, save system resources and improve system performance.
  • The external state in the flyweight mode is relatively independent and does not affect the internal state.

Disadvantages:
In order to make the object shareable, it is necessary to externalize part of the state of the flyweight object and separate the internal state and external state, which complicates the program logic.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131351716