Lying design pattern notes (XXIII) の Flyweight

Flyweight

definition

Use sharing to support a large number of fine-grained objects.

UML diagrams

For chestnuts

Among the actual development, a project if done well, if the market is opened up to the rest, then the core code can actually be universal, but with different data templates and features out of it, these are non-transparent to the user's. Minimum overhead to get the maximum benefit, it is all the company's purpose.

Talk is cheap, show me the code

(Shit less, grading up)

/**
 * 网站抽象类
 * Created by callmeDevil on 2019/12/15.
 */
public abstract class WebSite {
    public abstract void use(User user);
}
/**
 * 具体网站类
 * Created by callmeDevil on 2019/12/15.
 */
public class ConcreteWebSite extends WebSite{

    private String name = "";

    public ConcreteWebSite(String name){
        this.name = name;
    }

    @Override
    public void use(User user) {
        System.out.println("网站分类:" + name + " 用户:" + user.getName());
    }

}
/**
 * 网站工厂
 * Created by callmeDevil on 2019/12/15.
 */
public class WebSiteFactory {

    private HashMap<String, WebSite> flyweights = new HashMap<>();

    // 获得网站分类
    public WebSite getWebSiteCategory(String key){
        // 判断是否存在这个对象,如果存在则直接返回,若不存在则实例化再返回
        if (!flyweights.containsKey(key)) {
            flyweights.put(key, new ConcreteWebSite(key));
        }
        return flyweights.get(key);
    }

    // 获得网站分类总数
    public int getWebSiteCount(){
        return flyweights.size();
    }

}

/**
 * 用户
 * Created by callmeDevil on 2019/12/15.
 */
public class User {

    private String name;

    public User(String name){
        this.name = name;
    }

    // 省略 get set
}
public class Test {
    public static void main(String[] args) {
        WebSiteFactory factory = new WebSiteFactory();

        WebSite fx = factory.getWebSiteCategory("产品展示");
        fx.use(new User("路飞"));

        // 共享上方生成的对象,不再实例化
        WebSite fy = factory.getWebSiteCategory("产品展示");
        fy.use(new User("索隆"));

        WebSite fz = factory.getWebSiteCategory("产品展示");
        fz.use(new User("山治"));

        WebSite fl = factory.getWebSiteCategory("博客");
        fl.use(new User("娜美"));

        WebSite fm = factory.getWebSiteCategory("博客");
        fm.use(new User("乌索普"));

        WebSite fn = factory.getWebSiteCategory("博客");
        fn.use(new User("乔巴"));

        // 统计实例化个数,结果应为2
        System.out.println("网站分类总数为:" + factory.getWebSiteCount());
    }
}

operation result

网站分类:产品展示 用户:路飞
网站分类:产品展示 用户:索隆
网站分类:产品展示 用户:山治
网站分类:博客 用户:娜美
网站分类:博客 用户:乌索普
网站分类:博客 用户:乔巴
网站分类总数为:2

to sum up

benefit

Flyweight pattern can avoid the overhead of a large number of very similar class. In programming, the class may need to generate a large number of instances of fine-grained to represent data. If these examples can be found in addition to several parameters substantially the same, and sometimes it is possible to greatly reduce the need for the number of instantiated class subject. If those parameters can move outside the class instance, when they are passed in the method call, it can be greatly reduced by sharing a single instance number.

Usage scenarios

  • They should consider the application if the application uses a large number of objects, and a large number of these objects caused a lot of storage overhead
  • Most state of the object can be outside the state, if you delete an object outside the state, you can replace a lot of relatively small group of objects with shared objects, then you can consider the application.

Guess you like

Origin www.cnblogs.com/call-me-devil/p/12044428.html