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
    
  • 相关阅读:
    SOAP webserivce 和 RESTful webservice 对比及区别(转载)
    JavaWeb工程中web.xml基本配置(转载学习)
    iframe 自适应
    SQL分组求每组最大值问题的解决方法收集 (转载)
    关于试用jquery的jsonp实现ajax跨域请求数据的问题
    解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。
    Hadoop编译源码(面试重点)
    Hadoop学习(二)自己编译Hadoop安装包
    代理模式实现方式及优缺点对比
    zookeeper
  • 原文地址:https://www.cnblogs.com/zhangminghui/p/4214381.html
Copyright © 2011-2022 走看看