package AbstractFactoryPattern;/*** 抽象工厂模式** @author RXJ**/public class AbstractFactory {public void eat(KitchenFactory k) {System.out.println("A foodaholic is eating " + k.getFood().getFoodName() + " with " + k.getTableWare().getToolName());}public static void main(String[] args) {AbstractFactory fh = new AbstractFactory();KitchenFactory kf = new AKitchen();fh.eat(kf);}// 抽象食物interface Food {public String getFoodName();}// 抽象餐具interface TableWare {public String getToolName();}// 抽象工厂interface KitchenFactory {public Food getFood();public TableWare getTableWare();}// 具体食物 Apple 的定义如下static class Apple implements Food {public String getFoodName() {return "apple";}}// 具体餐具 Knife 的定义如下static class Knife implements TableWare {public String getToolName() {return "knife";}}// 以具体工厂 AKitchen 为例static class AKitchen implements KitchenFactory {public Food getFood() {return new Apple();}public TableWare getTableWare() {return new Knife();}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。