当前位置: 首页 > news >正文

《HeadFirst设计模式(第二版)》第四章代码——工厂模式

代码文件目录结构:

Cheese:

原料ingredient类中只以Cheese为例,不重复展示:

package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public abstract class Cheese {String name;String getName(){return name;}
}
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public class NYCheese extends Cheese{public NYCheese() {name = "NYCheese";}
}
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public class ChicagoCheese extends Cheese{public ChicagoCheese(){name = "ChicagoCheese";}
}
PizzaIngredientFactory
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public interface PizzaIngredientFactory {public Dough createDough();public Sauce createSauce();public Cheese createCheese();public Clams createClams();
}
NYPizzaIngredientFactory 
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public class NYPizzaIngredientFactory implements PizzaIngredientFactory{@Overridepublic Dough createDough() {return new NYDough();}public Sauce createSauce(){return new NYSauce();}@Overridepublic Cheese createCheese() {return new NYCheese();}@Overridepublic Clams createClams() {return new NYClams();}
}
ChicagoPizzaIngredientFactory
package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** @Author 竹心* @Date 2023/8/4**/public class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory{@Overridepublic Dough createDough() {return new ChicagoDough();}public Sauce createSauce(){return new ChicagoSauce();}@Overridepublic Cheese createCheese() {return new ChicagoCheese();}@Overridepublic Clams createClams() {return new ChicagoClams();}
}
Pizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.*;/*** @Author 竹心* @Date 2023/8/4**/public abstract class Pizza {String name;Dough dough;Sauce sauce;Cheese cheese;Clams clams;public abstract void prepare();public void bake(){System.out.println("Bake for 25 minutes at 350");}public void cut(){System.out.println("Cutting the pizza into diagonal slices");}public void box(){System.out.println("place pizza in official PizzaStore box");}public String getName(){return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Pizza{" +"name='" + name + '\'' +", dough=" + dough +", sauce=" + sauce +", cheese=" + cheese +", clams=" + clams +'}';}
}
CheesePizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;/*** @Author 竹心* @Date 2023/8/4**/public class CheesePizza extends Pizza{PizzaIngredientFactory ingredientFactory;public CheesePizza(PizzaIngredientFactory ingredientFactory){//在初始化的时候确定是哪一种品味(通过原料工厂)this.ingredientFactory = ingredientFactory;}@Overridepublic void prepare() {System.out.println("preparing "+name);dough = ingredientFactory.createDough();sauce = ingredientFactory.createSauce();cheese = ingredientFactory.createCheese();}
}
ClamPizza
package Chapter4_FactoryPattern.abstractFactoryPattern.Products;import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;/*** @Author 竹心* @Date 2023/8/4**/public class ClamPizza extends Pizza{PizzaIngredientFactory ingredientFactory;public ClamPizza(PizzaIngredientFactory ingredientFactory){this.ingredientFactory = ingredientFactory;}@Overridepublic void prepare() {System.out.println("preparing "+name);dough = ingredientFactory.createDough();sauce = ingredientFactory.createSauce();cheese = ingredientFactory.createCheese();clams = ingredientFactory.createClams();}
}
PizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;/*** @Author 竹心* @Date 2023/8/4**/public abstract class PizzaStore {public Pizza orderPizza(String type){Pizza pizza;pizza = createPizza(type);pizza.prepare();pizza.bake();pizza.cut();pizza.box();return pizza;}protected abstract Pizza createPizza(String type);}
NYPizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.NYPizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.CheesePizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.ClamPizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;/*** @Author 竹心* @Date 2023/8/4**/public class NYPizzaStore extends PizzaStore {protected Pizza createPizza(String item){Pizza pizza = null;PizzaIngredientFactory ingredientFactory =new NYPizzaIngredientFactory();if(item == "cheese"){pizza = new CheesePizza(ingredientFactory);pizza.setName("New York Style Cheese Pizza");}else if(item =="clam"){pizza = new ClamPizza(ingredientFactory);pizza.setName("New York Style Clam Pizza");}return pizza;}
}
ChicagoPizzaStore
package Chapter4_FactoryPattern.abstractFactoryPattern;import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.ChicagoPizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient.PizzaIngredientFactory;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.CheesePizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.ClamPizza;
import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;/*** @Author 竹心* @Date 2023/8/4**/public class ChicagoPizzaStore extends PizzaStore {@Overrideprotected Pizza createPizza(String item) {Pizza pizza = null;PizzaIngredientFactory ingredientFactory =new ChicagoPizzaIngredientFactory();if(item == "cheese"){pizza = new CheesePizza(ingredientFactory);pizza.setName("Chicago Style Cheese Pizza");}else if(item =="clam"){pizza = new ClamPizza(ingredientFactory);pizza.setName("Chicago Style Clam Pizza");}return pizza;}
}
PizzaTestDrive
package Chapter4_FactoryPattern.abstractFactoryPattern;import Chapter4_FactoryPattern.abstractFactoryPattern.Products.Pizza;/*** @Author 竹心* @Date 2023/8/4**/public class PizzaTestDrive {public static void main(String[] args) {PizzaStore nyStore = new NYPizzaStore();PizzaStore chicagoStore = new ChicagoPizzaStore();Pizza pizza = nyStore.orderPizza("cheese");System.out.println("Ethan ordered a "+pizza.getName()+'\n');pizza = chicagoStore.orderPizza("cheese");System.out.println("Joel ordered a "+pizza.getName()+'\n');}
}
notes:
简单工厂模式:就是将平常的new过程封装成一个类来实现工厂方法模式:定义一个创建对象的接口,但是由它的子类决定要实例化哪个类。工厂方法让类把实例化推迟到子类这里的“决定”其实是表示用哪个接口子类来创建产品。抽象工厂模式:提供一个接口,用于创建相关或者依赖对象的家族,而不必指定他们的具体类
运行结果:

 

http://www.lryc.cn/news/112617.html

相关文章:

  • 拖拽宫格vue-grid-layout详细应用及案例
  • sanyo三洋摄像机卡有部分坏块恢复案例
  • 【C++】STL——set和map及multiset和multiset的介绍及使用
  • 帕累托森林:IEEE Fellow唐远炎院士出任「儒特科技」首席架构官
  • 数据库大数据
  • 骨传导耳机是怎么工作的?骨传导耳机是智商税产品吗?
  • Java电子招投标采购系统源码-适合于招标代理、政府采购、企业采购、等业务的企业tbms
  • 算法-合并区间
  • 布基纳法索ECTN(BESC)申请流程
  • CDN安全面临的问题及防御架构
  • 【MySQL】MySQL管理 (十四)
  • Mybatis:一对一查询映射处理
  • 九、用 ChatGPT 提高算法和编程能力
  • 【数模】主成分分析PCA
  • 全志F1C200S嵌入式驱动开发(从DDR中截取内存)
  • C++中点云聚类算法的实现与应用探索
  • 大数据Flink(五十六):Standalone伪分布环境(开发测试)
  • Godot 4 源码分析 - 碰撞
  • 前端面试经典算法题
  • ospf减少LSA更新
  • 万字长文解析深度学习中的术语
  • 冠达管理投资前瞻:三星加码机器人领域 大信创建设提速
  • 24届近5年上海交通大学自动化考研院校分析
  • 【PDF密码】PDF文件不能打印,为什么?
  • LeetCode-Java(03)
  • 【Linux命令行与Shell脚本编程】第十六章 Shell函数
  • SpringCloud-Hystrix服务熔断与降级工作原理源码 | 京东物流技术团队
  • (一)react脚手架
  • Typescript中的元组与数组的区别
  • SpringBoot的index首页的访问、自定义Favicon图标