zoukankan      html  css  js  c++  java
  • 设计模式之享元模式 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 }
    测试调用
  • 相关阅读:
    团队项目-第一阶段冲刺7
    团队项目-第一阶段冲刺6
    Spring Boot 揭秘与实战(七) 实用技术篇
    Spring Boot 揭秘与实战(七) 实用技术篇
    Spring Boot 揭秘与实战(六) 消息队列篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(五) 服务器篇
    Spring Boot 揭秘与实战(四) 配置文件篇
  • 原文地址:https://www.cnblogs.com/qingdaofu/p/7473245.html
Copyright © 2011-2022 走看看