/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 享元模式 Flyweight Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Order.java * * */ package com.javapatterns.flyweight; /** *抽象对象 * @author geovindu * */ public abstract class Order { /** *将咖啡卖管人 * */ public abstract void serve(Table table); /** *返回咖啡的名字 * @return * */ public abstract String getFlavor(); }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 享元模式 Flyweight Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Table.java * * */ package com.javapatterns.flyweight; /** * * @author geovindu * */ public class Table { private int number; /** * * */ public Table(int number) { this.number = number; } /** * * */ public void setNumber(int number) { this.number = number; } /** * * */ public int getNumber() { return number; } }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 享元模式 Flyweight Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc Flavor.java * * */ package com.javapatterns.flyweight; /** *具体对象 * @author geovindu * */ public class Flavor extends Order{ private String flavor; /** *构造 * */ public Flavor(String flavor) { this.flavor = flavor; } /** *返回咖啡名称 * */ public String getFlavor() { return this.flavor; } /** *将咖啡卖给客人 * */ public void serve(Table table) { System.out.println("Serving table " + table.getNumber() + " with flavor " + flavor ); } }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 享元模式 Flyweight Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc FlavorFactory.java * * */ package com.javapatterns.flyweight; /** *工厂对象 * @author geovindu * * */ public class FlavorFactory { private Order[] flavors = new Flavor[10]; private int ordersMade = 0; private int totalFlavors = 0 ; /** *工厂方法,根据所需的风味提供咖啡 * * */ public Order getOrder(String flavorToGet) { if (ordersMade > 0) { for (int i = 0; i < ordersMade; i++) { if (flavorToGet.equals((flavors[i]).getFlavor())) { return flavors[i]; } } } flavors[ordersMade] = new Flavor(flavorToGet); totalFlavors++; return flavors[ordersMade++]; } /** *辅助方法,返回创建过的风味对象的个数 * * */ public int getTotalFlavorsMade() { return totalFlavors; } }
/** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * 享元模式 Flyweight Patterns * 历史版本: JDK 14.02 * 2022-09-12 创建者 geovindu * 2022-09-12 添加 Lambda * 2022-09-12 修改:date * 接口类 * 2022-09-12 修改者:Geovin Du * 生成API帮助文档的指令: *javadoc - -encoding Utf-8 -d apidoc ClientSingleton.java * * */ package com.javapatterns.flyweight; import java.util.List; /** * * @author geovindu * * */ public class ClientOrder { //the flavors ordered /** * * */ public ClientOrder() { } /** * @label Creates */ private Order[] flavors = new Flavor[100]; private int ordersMade = 0; private FlavorFactory flavorFactory; /** @link dependency */ /*#Table lnkTable;*/ private void takeOrders(String aFlavor) { flavors[ordersMade++] = flavorFactory.getOrder(aFlavor); } /** * * @param orders * */ public void OrderShow(List<String> orders) { //创建风味咖啡工厂对象 flavorFactory = new FlavorFactory(); //创建一个咖啡对象 orders.forEach(item-> { takeOrders(item); }); //创建所有咖啡对象卖给客人 for (int i = 0; i < ordersMade; i++) { flavors[i].serve(new Table(i)); } //风味对象种类数 System.out.println("\nTotal teaFlavor objects made: " + flavorFactory.getTotalFlavorsMade()); } }
调用测试:
//享元模式 List<String> orders=new LinkedList<>(); orders.add("Black Coffee"); orders.add("Capucino"); orders.add("Espresso"); orders.add("Espresso"); orders.add("Capucino"); orders.add("Capucino"); orders.add("Black Coffee"); orders.add("Espresso"); orders.add("Capucino"); orders.add("Black Coffee"); orders.add("Espresso"); ClientOrder clientOrder=new ClientOrder(); clientOrder.OrderShow(orders);
输出:
Serving table 0 with flavor Black Coffee Serving table 1 with flavor Capucino Serving table 2 with flavor Espresso Serving table 3 with flavor Espresso Serving table 4 with flavor Capucino Serving table 5 with flavor Capucino Serving table 6 with flavor Black Coffee Serving table 7 with flavor Espresso Serving table 8 with flavor Capucino Serving table 9 with flavor Black Coffee Serving table 10 with flavor Espresso Total teaFlavor objects made: 3
标签:12,java,Flyweight,09,orders,Patterns,2022,flavor,public From: https://www.cnblogs.com/geovindu/p/16705588.html