zoukankan      html  css  js  c++  java
  • [译]ava 设计模式之享元

    (文章翻译自Java Design Pattern: Flyweight)

    享元模式用于最小化内存开销。它做的就是使用其他相似的对象尽可能多的分享数据。

    1.享元模式类图

    flyweight-pattern-class-diagram

    2.享元模式Java代码

    // Flyweight object interface
    interface ICoffee {
        public void serveCoffee(CoffeeContext context);
    }
    
    // Concrete Flyweight object  
    class Coffee implements ICoffee {
        private final String flavor;
     
        public Coffee(String newFlavor) {
            this.flavor = newFlavor;
            System.out.println("Coffee is created! - " + flavor);
        }
     
        public String getFlavor() {
            return this.flavor;
        }
     
        public void serveCoffee(CoffeeContext context) {
            System.out.println("Serving " + flavor + " to table " + context.getTable());
        }
    }
    
    // A context, here is table number
    class CoffeeContext {
       private final int tableNumber;
     
       public CoffeeContext(int tableNumber) {
           this.tableNumber = tableNumber;
       }
     
       public int getTable() {
           return this.tableNumber;
       }
    }
    

    CoffeeFactory:它只是在需要的时候才会创建一个新的咖啡。

    //The FlyweightFactory!
    class CoffeeFactory {
     
        private HashMap<String, Coffee> flavors = new HashMap<String, Coffee>();
     
        public Coffee getCoffeeFlavor(String flavorName) {
            Coffee flavor = flavors.get(flavorName);
            if (flavor == null) {
                flavor = new Coffee(flavorName);
                flavors.put(flavorName, flavor);
            }
            return flavor;
        }
     
        public int getTotalCoffeeFlavorsMade() {
            return flavors.size();
        }
    }
    

    //服务员上咖啡

    public class Waitress {
       //coffee array
       private static Coffee[] coffees = new Coffee[20];
       //table array
       private static CoffeeContext[] tables = new CoffeeContext[20];
       private static int ordersCount = 0;
       private static CoffeeFactory coffeeFactory;
     
       public static void takeOrder(String flavorIn, int table) {
    	   coffees[ordersCount] = coffeeFactory.getCoffeeFlavor(flavorIn);
           tables[ordersCount] = new CoffeeContext(table);
           ordersCount++;
       }
     
       public static void main(String[] args) {
    	   coffeeFactory = new CoffeeFactory();
     
           takeOrder("Cappuccino", 2);
           takeOrder("Cappuccino", 2);
           takeOrder("Regular Coffee", 1);
           takeOrder("Regular Coffee", 2);
           takeOrder("Regular Coffee", 3);
           takeOrder("Regular Coffee", 4);
           takeOrder("Cappuccino", 4);
           takeOrder("Cappuccino", 5);
           takeOrder("Regular Coffee", 3);
           takeOrder("Cappuccino", 3);
     
           for (int i = 0; i < ordersCount; ++i) {
        	   coffees[i].serveCoffee(tables[i]);
           }
     
           System.out.println("
    Total Coffee objects made: " +  coffeeFactory.getTotalCoffeeFlavorsMade());
       }
    }
    

    检查下面的结果输出,虽然有十个桌子需要咖啡可是只生产了两种咖啡。

    Coffee is created! - Cappuccino
    Coffee is created! - Regular Coffee
    Serving Cappuccino to table 2
    Serving Cappuccino to table 2
    Serving Regular Coffee to table 1
    Serving Regular Coffee to table 2
    Serving Regular Coffee to table 3
    Serving Regular Coffee to table 4
    Serving Cappuccino to table 4
    Serving Cappuccino to table 5
    Serving Regular Coffee to table 3
    Serving Cappuccino to table 3
    
    Total Coffee objects made: 2
    
  • 相关阅读:
    [Python 多线程] 详解daemon属性值None,False,True的区别 (五)
    Python 多线程 线程安全、daemon简介 (四)
    Python 多线程 start()和run()方法的区别(三)
    jstack的使用方法
    java中的fork-join框架
    RabbitMQ:消息发送确认 与 消息接收确认(ACK)
    vue项目搭建
    Node.js安装及环境配置之Windows篇
    Storm里面fieldsGrouping和Field的概念详解
    Java8 如何正确使用 Optional
  • 原文地址:https://www.cnblogs.com/zhangminghui/p/4214381.html
Copyright © 2011-2022 走看看