• 20、FlyWeight 享元模式


    1Flyweight

    使

    使 12345

    使 12

    2

    使

    IgoChessmanBlackIgoChessmanWhiteIgoChessmanIgoChessmanFactory

     import java.util.*;  
     
     //  
     abstract class IgoChessman {  
         public abstract String getColor();  
     
         public void display() {  
             System.out.println("" + this.getColor());    
        }  
     }  
     
     //  
     class BlackIgoChessman extends IgoChessman {  
         public String getColor() {  
             return "";  
        }    
     }  
     
     //  
     class WhiteIgoChessman extends IgoChessman {  
         public String getColor() {  
             return "";  
        }  
     }  
     
     //使  
     class IgoChessmanFactory {   
        private static IgoChessmanFactory instance = new IgoChessmanFactory();   
        private static Hashtable ht; //使Hashtable    

        private IgoChessmanFactory() {   
            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;   
       }    

        //keyHashtable   
        public static IgoChessman getIgoChessman(String color) {   
            return (IgoChessman)ht.get(color);     
       }   
    }

     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.display();   
            black2.display();   
            black3.display();   
            white1.display();   
            white2.display();   
       }   
    }

     true
     true
     
     
     
     
     

    使

    CoordinatesIgoChessmandisplay()CoordinatesCoordinatesIgoChessman

     //  
     class Coordinates {  
         private int x;  
         private int y;  
     
         public Coordinates(int x,int y) {  
             this.x = x;  
             this.y = y;  
        }  
     
         public int getX() {  
             return this.x;  
        }  
     
         public void setX(int x) {  
             this.x = x;  
        }  
     
         public int getY() {  
             return this.y;  
        }  
     
         public void setY(int y) {  
             this.y = y;  
        }  
     }  
     
     //  
     abstract class IgoChessman {  
         public abstract String getColor();  
     
         public void display(Coordinates coord){   
            System.out.println("" + this.getColor() + "" + coord.getX() + "" + coord.getY() );     
       }   
    }

     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.display(new Coordinates(1,2));   
           black2.display(new Coordinates(3,4));   
           black3.display(new Coordinates(1,3));   
           white1.display(new Coordinates(2,5));   
           white2.display(new Coordinates(2,4));   
       }   
    }

     true
     true
     12
     34
     13
     25
     24

    display()

    3flyweight

    Flyweight ()FlyweightBigChar

    FlyweightFactory ()FlyweightFactoryFlyweightFlyweightBigCharFactory

    Client ()Client使FlyweightFactoryFlyweightBigString

    4

    使

    (1)

    (2)

    (3) 使使使

    使

    (1)

    (2)

    (3) 使使使

    公众号发哥讲

    这是一个稍偏基础和偏技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。

    img

    如果你觉得文章还不错,就请点击右上角选择发送给朋友或者转发到朋友圈~

    ● 扫码关注我们

    据说看到好文章不推荐的人,服务器容易宕机!

    本文版权归发哥讲博客园共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

     

  • 相关阅读:
    Hibernate + mysql 查询伪劣时:= 出现 Space is not allowed after parameter prefix ':' MySQL异常
    Linux下使用Markdown
    第十六章:Java内存模型——Java并发编程实战
    第十五章:原子变量与非阻塞机制——Java并发编程实战
    第十四章:构建自定义的同步工具——Java并发编程实战
    第十三章:显示锁——Java并发编程实战
    访问者模式——HeadFirst设计模式学习笔记
    原型模式——HeadFirst设计模式学习笔记
    第十二章:并发程序的测试——Java并发编程实战
    备忘录模式——HeadFirst设计模式学习笔记
  • 原文地址:https://www.cnblogs.com/naimao/p/13446540.html
Copyright © 2020-2023  润新知