12 Sep 2014

Java设计模式:享元模式

注:本文为译文,原文出处java-design-patterns-in-stories

享元模式被用于减少内存使用情况. 它所做的就是与其他相似的对象分享尽量多的数据。

1.享元模式类图

flyweight-pattern-class-diagram

2.享元模式Java代码

// 享元对象接口
interface ICoffee {
    public void serveCoffee(CoffeeContext context);
}
// 具体的享元对象
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());
    }
}
// 一个上下文环境, 此处是桌号
class CoffeeContext {
   private final int tableNumber;

   public CoffeeContext(int tableNumber) {
       this.tableNumber = tableNumber;
   }

   public int getTable() {
       return this.tableNumber;
   }
}

CoffeeFactory: 只在必要创建一个新的coffee对象.

//享元工厂!
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("\nTotal Coffee objects made: " +  coffeeFactory.getTotalCoffeeFlavorsMade());
   }
}

检查下面的输出, 咖啡供应了10桌, 但是一共只创建了2个咖啡对象。

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

例子是基于维基百科中的享元模式, 并被改善的更易于理解。


Tags:
0 comments