二十三种设计模式之享元模式十一

   日期:2020-09-06     作者:风筱    浏览:118    评论:0    
核心提示:享元模式package ink.yql.flyweight;public interface Flyweight { String say();}package ink.yql.flyweight;public class FlyweightImpl01 implements Flyweight{ private String

享元模式

package ink.yql.flyweight;


public interface Flyweight {

    
    String say();

}
package ink.yql.flyweight;

public class FlyweightImpl01 implements  Flyweight{

    private String name;

    @Override
    public String say() {
        return name;
    }

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

}
package ink.yql.flyweight;

import java.util.HashMap;
import java.util.Map;

public class FlyweihtFactory {
    
  private  static  Map<String, Flyweight> map = new HashMap<>();
    
    static Flyweight  getInstance(String name){
        Flyweight flyweight = map.get(name);
        if (flyweight == null){
            FlyweightImpl01 flyweightImpl01 = new FlyweightImpl01(name);
            map.put(name,flyweightImpl01);
            return  flyweightImpl01;
        }
        return  flyweight;
    }
}

测试

package ink.yql.flyweight;

import ink.yql.factory02.FlipPhoneFactory;

public class Test01 {
    public static void main(String[] args) {
        Flyweight a1 = FlyweihtFactory.getInstance("a");
        Flyweight a2 = FlyweihtFactory.getInstance("a");
        Flyweight a3 = FlyweihtFactory.getInstance("a3");
        Flyweight b = FlyweihtFactory.getInstance("b");
        System.out.println(a1);
        System.out.println(a2);
        System.out.println(a3);
        System.out.println(b);

    }
}
 
打赏
本文转载自:https://blog.csdn.net/weixin_46503529/article/details/108422333  
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服