• 软件设计享元模式


    设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。

    package com;
    
    
    class BlackIgoChessman extends IgoChessman{
     
        @Override
        public String getColor() {
            // TODO Auto-generated method stub
            return "黑色";
        }
     
    }
    BlackIgoChessman
    package com;
    
    
    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;
        }
        
    }
    Coordinates
    package com;
    
    
    abstract class IgoChessman {
        
        public abstract String getColor();
        public void locate(Coordinates coord){
            System.out.println("棋子颜色:"+this.getColor()+",棋子位置:"+coord.getX()+","+coord.getY());
        }
    }
    IgoChessman
    package com;
    
    import java.util.Hashtable;
    
    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);
        }
    }
    IgoChessmanFactory
    package com;
    
    class WhiteIgoChessman extends IgoChessman{
         
        @Override
        public String getColor() {
            // TODO Auto-generated method stub
            return "白色";
        }
     
    }
    WhiteIgoChessman
    package com;
    
    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));
        }
    }
    Client
  • 相关阅读:
    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
    js 中怎么使 if(aᅠ==1 && a== 2 && ᅠa==3) 返回 true?
    最新Hadoop-2.7.2+hbase-1.2.0+zookeeper-3.4.8 HA高可用集群配置安装
    spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置(二)+Druid连接池
    activiti 学习由浅入深
    hadoop2.4.1+hbase0.98.3实现的分布式网盘系统初步(已开源)
    【Note】Linux
    记初学CMMI,跳出码农搬砖时代,人人都是经营者
    java8_接口中的默认方法与静态方法
    java8_Stream
  • 原文地址:https://www.cnblogs.com/feng747/p/15579393.html
Copyright © 2020-2023  润新知