El patrón Flyweight se utiliza para minimizar el uso de memoria. Lo que hace es compartir la mayor cantidad de datos posible con otros objetos similares.
1. Diagrama de clases de patrón de peso mosca
2. Código Java de patrón de peso mosca
// 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: solo crea un nuevo café cuando es necesario.
//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(); } } |
// Camarera sirviendo café
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()); } } |
Vea el resultado a continuación, el café se sirve en 10 mesas, ¡pero solo se crean 2 cafés!
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
Este ejemplo se cambia en función de wiki-flyweight *, se mejora para que sea más comprensible.
* http://en.wikipedia.org/wiki/Flyweight_pattern