• 设计模式之享元模式 FlyWeight


     代码实现

     1 /**
     2  * 享元工厂类
     3  * @author bzhx
     4  * 2017年3月14日
     5  */
     6 public class ChessFlyWeightFactory {
     7     //享元池
     8     private static Map<String,ChessFlyWeight> map = new HashMap<String,ChessFlyWeight>();
     9     public static ChessFlyWeight getChess(String color){
    10         if(map.get(color)!=null){
    11             return map.get(color);
    12         }else{
    13             ChessFlyWeight cfw = new ConcreteChess(color);
    14             map.put(color, cfw);
    15             return cfw;
    16         }
    17     }
    18 }
    享元工厂类
    1 public interface ChessFlyWeight {
    2 
    3     void setColor(String c);
    4     String getColor();
    5     void display(Coordinate c);
    6 }
    抽象享元类
     1 //内部状态ConcreteFlyWeight
     2 class ConcreteChess implements ChessFlyWeight{
     3 
     4     private String color;
     5     
     6     public ConcreteChess(String color) {
     7         super();
     8         this.color = color;
     9     }
    10 
    11     @Override
    12     public void setColor(String c) {
    13         this.color = c;
    14     }
    15 
    16     @Override
    17     public String getColor() {
    18         return color;
    19     }
    20 
    21     @Override
    22     public void display(Coordinate c) {
    23         System.out.println("棋子颜色:"+color);
    24         System.out.println("棋子位置:"+c.getX()+"-----"+c.getY());
    25     }
    26     
    27 }
    具体享元类(内部共享)
     1 /**
     2  * 外部状态 UnSharedConcreteFlyWeight
     3  * @author bzhx
     4  * 2017年3月14日
     5  */
     6 public class Coordinate {
     7 
     8     private int x,y;
     9 
    10     public Coordinate(int x, int y) {
    11         super();
    12         this.x = x;
    13         this.y = y;
    14     }
    15 
    16     public int getX() {
    17         return x;
    18     }
    19 
    20     public void setX(int x) {
    21         this.x = x;
    22     }
    23 
    24     public int getY() {
    25         return y;
    26     }
    27 
    28     public void setY(int y) {
    29         this.y = y;
    30     }
    31     
    32     
    33     
    34 }
    非共享享元类
     1 public class Client {
     2 
     3     public static void main(String[] args) {
     4         ChessFlyWeight chess1 = ChessFlyWeightFactory.getChess("黑色");
     5         ChessFlyWeight chess2 = ChessFlyWeightFactory.getChess("黑色");
     6         System.out.println(chess1);
     7         System.out.println(chess2);
     8         
     9         System.out.println("增加外部状态的处理============================");
    10         chess1.display(new Coordinate(10,10));
    11         chess2.display(new Coordinate(20,20));
    12     }
    13 
    14 }
    测试调用
  • 相关阅读:
    复制文件-用FileOutputStream和FileInputStream读写文件
    向属性文件中添加属性
    合并多个文件的内容
    EVA4400存储RAID信息丢失数据恢复过程
    DELL Eq PS4000数据恢复成功案例
    华为OceanStor S5600T服务器数据恢复
    IBM ds4700崩溃重组raid及修复数据库
    分析IBM AIX存储层结构 / 常用存储命令整理
    riad5阵列崩溃,恢复数据过程
    如何使用Handy Backup 6.2进行数据备份(步骤阅读)
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7473245.html
Copyright © 2020-2023  润新知