zoukankan      html  css  js  c++  java
  • 设计模式享元模式

    类图

     

     

    源码

    package weight;
    
    /**
     * 坐标类:外部状态类
     * @author fly
     *
     */
    class Coordinates {
        
        private int x;
        private int y;
        
        public Coordinates(int x,int y) {
            // TODO Auto-generated constructor stub
            this.x = x;
            this.y = y;
        }
     
        public int getX() {
            return x;
        }
     
        public void setX(int x) {
            this.x = x;
        }
     
        public int getY() {
            return y;
        }
     
        public void setY(int y) {
            this.y = y;
        }
        
    }
    (2)    抽象享元类
    package weight;
    
    /**
     * 围棋棋子类:抽象享元类
     * @author fly
     *
     */
    abstract class IgoChessman {
        
        public abstract String getColor();
        public void locate(Coordinates coord){
            System.out.println("棋子颜色:"+this.getColor()+",棋子位置:"+coord.getX()+","+coord.getY());
        }
    }
    /**
     * 黑色棋子类:具体享元类
     * @author fly
     *
     */
    class BlackIgoChessman extends IgoChessman{
     
        @Override
        public String getColor() {
            // TODO Auto-generated method stub
            return "黑色";
        }
     
    }
    /**
     * 白色棋子类:具体享元类
     * @author fly
     *
     */
    class WhiteIgoChessman extends IgoChessman{
     
        @Override
        public String getColor() {
            // TODO Auto-generated method stub
            return "白色";
        }
     
    }
    
    (3)    享元工厂类
    package weight;
    
    import java.util.Hashtable;
    
    /**
     * 围棋棋子工厂类:享元工厂类
     * @author fly
     *
     */
    public class IgoChessmanFactory {
        
        private static IgoChessmanFactory instance = new IgoChessmanFactory();
        private static Hashtable ht;
        
        public IgoChessmanFactory() {
            // TODO Auto-generated constructor stub
            ht = new Hashtable();
            IgoChessman black,white;
            black = new BlackIgoChessman();
            ht.put("b", black);
            white = new WhiteIgoChessman();
            ht.put("w", white);
        }
        
        public static IgoChessmanFactory getInstance(){
            return instance;
        }
        
        public static IgoChessman getIgoChessman(String color){
            return (IgoChessman)ht.get(color);
        }
    }
    (4)    测试类
    package weight;
    
    /**
     * 客户端测试类
     * @author fly
     *
     */
    public class Client {
        
        public static void main(String[] args) {
            IgoChessman black1,black2,black3,white1,white2;
            IgoChessmanFactory factory;
            factory = IgoChessmanFactory.getInstance();
            black1 = factory.getIgoChessman("b");
            black2 = factory.getIgoChessman("b");
            black3 = factory.getIgoChessman("b");
            System.out.println("判断两颗黑棋是否相同:"+(black1==black2));
            white1 = factory.getIgoChessman("w");
            white2 = factory.getIgoChessman("w");
            System.out.println("判断两颗白棋是否相同:"+(white1==white2));
            black1.locate(new Coordinates(1, 2));
            black2.locate(new Coordinates(3, 4));
            black3.locate(new Coordinates(1, 3));
            white1.locate(new Coordinates(2, 5));
            white2.locate(new Coordinates(2, 4));
        }
    }

     

    测试截图

     

  • 相关阅读:
    数据库内连接、外连接与自连接
    安装MySQL容易出现的问题
    安装MySQL时提示3306端口已被占用的解决方案
    Smoke Testing
    冒烟测试与BVT测试
    以操作系统的角度述说线程与进程
    Notepad++配置Python开发环境
    Notepad++使用教程
    Sublime Text 皮肤插件安装
    小狼毫输入法常用设置
  • 原文地址:https://www.cnblogs.com/lx06/p/15688465.html
Copyright © 2011-2022 走看看