当前位置: 首页 > 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过程封装成一个类来实现工厂方法模式:定义一个创建对象的接口,但是由它的子类决定要实例化哪个类。工厂方法让类把实例化推迟到子类这里的“决定”其实是表示用哪个接口子类来创建产品。抽象工厂模式:提供一个接口,用于创建相关或者依赖对象的家族,而不必指定他们的具体类
运行结果:

 

相关文章:

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

代码文件目录结构: Cheese: 原料ingredient类中只以Cheese为例,不重复展示: package Chapter4_FactoryPattern.abstractFactoryPattern.Ingredient;/*** Author 竹心* Date 2023/8/4**/public abstract class Cheese {String name;String g…...

拖拽宫格vue-grid-layout详细应用及案例

文章目录 1、前言2、安装3、属性4、事件5、占位符样式修改6、案例 1、前言 vue-grid-layout是一个适用于vue的拖拽栅格布局库,功能齐全,适用于拖拽高度/宽度自由调节的布局需求,本文将讲述一些常用参数和事件,以及做一个同步拖拽…...

sanyo三洋摄像机卡有部分坏块恢复案例

sanyo三洋,这个品牌的摄像机真的是罕见。实际上日系摄像机领域就两个公司,一个是佳能一个索尼,其它的都厂商基本上全是用这两家公司的方案,当然松下这个怪咖除外!下面我们看看三洋的恢复案例。 故障存储:16G SD卡 故…...

【C++】STL——set和map及multiset和multiset的介绍及使用

🚀 作者简介:一名在后端领域学习,并渴望能够学有所成的追梦人。 🚁 个人主页:不 良 🔥 系列专栏:🛸C 🛹Linux 📕 学习格言:博观而约取&#xff0…...

帕累托森林:IEEE Fellow唐远炎院士出任「儒特科技」首席架构官

导语 「儒特科技」作为一家拥有全球独创性极致化微内核Web引擎架构的前沿科技企业,从成立即受到中科院软件所和工信部的重点孵化及扶持,成长异常迅速。前不久刚正式官方融入中国五大根操作系统体系,加速为其下游上千家相关衍生OS和应用软件企…...

数据库大数据

数据库 PyMongo模块的使用-MongoDB的Python接口 MapReduce将数据分解成子集,在不同机器上分开处理,并把结果集合起来,从而处理大数据的泛化框架。 Hadoop是MapReduce的一种实现,类似于C++是面向对象编程的实现一样。 NoSQL-Not Only SQL,技能能更新颖,更高效地访问(如…...

骨传导耳机是怎么工作的?骨传导耳机是智商税产品吗?

骨传导耳机是怎么工作的?骨传导耳机是智商税产品吗? 骨传导耳机是怎么工作的? 骨传导耳机的传声方式跟传统耳机完全不同,骨传导耳机就是利用骨传导的原理是直接将人体骨结构作为传声介质,通过颅骨来进行声音传播的&am…...

Java电子招投标采购系统源码-适合于招标代理、政府采购、企业采购、等业务的企业tbms

​ 功能描述 1、门户管理:所有用户可在门户页面查看所有的公告信息及相关的通知信息。主要板块包含:招标公告、非招标公告、系统通知、政策法规。 2、立项管理:企业用户可对需要采购的项目进行立项申请,并提交审批,查…...

算法-合并区间

以数组 intervals 表示若干个区间的集合,其中单个区间为 intervals[i] [starti, endi] 。请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。 输入:intervals [[1,3],[2,6],[8,10],[15,…...

布基纳法索ECTN(BESC)申请流程

根据BURKINA FASO布基纳法索签发于 11/07/2006法令编号 00557的规定: 自2006年11月07 日起所有出口至布基纳法索(Burkina Faso)的货物,必须申请ECTN/BESC。ECTN是ELECTRONIC CARGO TRACKING NOTE的英文缩写,BESC是BORDEREAU DE SU…...

CDN安全面临的问题及防御架构

CDN安全 SQL注入攻击(各开发小组针对密码和权限的管理,和云安全部门的漏洞扫描和渗透测试) Web Server的安全(运营商和云安全部门或者漏洞纰漏第三方定期发布漏洞报告修复,例如:nginx版本号和nginx resol…...

【MySQL】MySQL管理 (十四)

🚗MySQL学习第十四站~ 🚩本文已收录至专栏:MySQL通关路 ❤️文末附全文思维导图,感谢各位点赞收藏支持~ 一.系统数据库 MySQL8.0数据库安装完成后,自带了一下四个数据库,具体作用如下: 数据含…...

Mybatis:一对一查询映射处理

Mybatis:一对一查询映射处理 前言一、概述二、创建数据模型三、 问题四、解决方案1、方案一:级联方式处理映射关系2、方案二:使用association处理映射关系3、方案三:分步查询 前言 本博主将用CSDN记录软件开发求学之路上亲身所得…...

九、用 ChatGPT 提高算法和编程能力

目录 一、实验介绍 二、背景 三、LeetCode 刷题带来的问题 四、ChatGPT 如何帮助刷题 五、ChatGPT 推荐学习资源...

【数模】主成分分析PCA

主成分分析(Principal Component Analysis,PCA),是一种降维算法,它能将多个指标转换为少数几个主成分,这些主成分是原始变量的线性组合,且彼此之间互不相关,其能反映出原始数据的大部分信息。使用场景:一般…...

全志F1C200S嵌入式驱动开发(从DDR中截取内存)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】 linux内核起来的时候,不一定所有的内存都是分配给linux使用的。有的时候,我们是希望能够截留一部分内存的。为什么保留这部分内存呢?这里面可以有很多的用途。比如说,第一,如果…...

C++中点云聚类算法的实现与应用探索

第一部分:C中点云聚类算法的实现与应用 在当今的计算机视觉领域,点云数据是一种重要的三维数据类型,它能有效表达三维物体的形状信息。然而,由于点云数据的无序性和稀疏性,对其进行分析与处理的难度较大。本文将介绍如…...

大数据Flink(五十六):Standalone伪分布环境(开发测试)

文章目录 Standalone伪分布环境(开发测试) 一、架构图 二、环境准备 三、下载安装包</...

Godot 4 源码分析 - 碰撞

碰撞功能应该是一个核心功能&#xff0c;它能自动产生相应的数据&#xff0c;比如目标对象进入、离开本对象的检测区域。 基于属性设置&#xff0c;能碰撞的都具备这样的属性&#xff1a;Layer、Mask. 在Godot 4中&#xff0c;Collision属性中的Layer和Mask属性是用于定义碰撞…...

前端面试经典算法题

前言 现在面试流行考核算法&#xff0c;做过面试官&#xff0c;也被面试。问算法对面试官来说&#xff0c;是一种解脱&#xff0c;找出了一个看似很高明且能偷懒的办法选择人&#xff0c;避免了不知道问啥的尴尬&#xff1b;被面试者&#xff0c;也找到了一种新的面试八股文&am…...

ospf减少LSA更新

实验及实验要求 一、思路 1.根据区域划分IP地址 2.使公网可通---写缺省 3.使R3成为MGRE中心站点&#xff0c;R5、R6、R7为分支站点 4.一个个去配置ospf区域和RIP区域&#xff0c;确保每个区域配置无误 5.区域0要更改OSPF在接口的工作类型为broadcast &#xff0c;并使R3为…...

万字长文解析深度学习中的术语

引言 新手在学习深度学习或者在看深度学习论文的过程中&#xff0c;有不少专业词汇&#xff0c;软件翻译不出来&#xff0c;就算是翻译出来也看不懂&#xff0c;因为不少术语是借用其他学科的概念&#xff0c;这里整理了一些在深度学习中常见的术语&#xff0c;并对一些概念进…...

冠达管理投资前瞻:三星加码机器人领域 大信创建设提速

上星期五&#xff0c;沪指高开高走&#xff0c;盘中一度涨超1%打破3300点&#xff0c;但随后涨幅收窄&#xff1b;深成指、创业板指亦强势震动。截至收盘&#xff0c;沪指涨0.23%报3288.08点&#xff0c;深成指涨0.67%报11238.06点&#xff0c;创业板指涨0.95%报2263.37点&…...

24届近5年上海交通大学自动化考研院校分析

今天给大家带来的是上海交通大学控制考研分析 满满干货&#xff5e;还不快快点赞收藏 一、上海交通大学 学校简介 上海交通大学是我国历史最悠久、享誉海内外的高等学府之一&#xff0c;是教育部直属并与上海市共建的全国重点大学。经过120多年的不懈努力&#xff0c;上海交…...

【PDF密码】PDF文件不能打印,为什么?

正常的PDF文件是可以打印的&#xff0c;如果PDF文件打开之后发现文件不能打印&#xff0c;我们需要先查看一下自己的打印机是否能够正常运行&#xff0c;如果打印机是正常的&#xff0c;我们再查看一下&#xff0c;文件中的打印功能按钮是否是灰色的状态。 如果PDF中的大多数功…...

LeetCode-Java(03)

9. 回文数 class Solution {public boolean isPalindrome(int x) {if (x < 0 || (x % 10 0 && x ! 0)) {return false;}int revertedNumber 0;while (x > revertedNumber) {revertedNumber revertedNumber * 10 x % 10;x / 10;}// 当长度为奇数时通过reverte…...

【Linux命令行与Shell脚本编程】第十六章 Shell函数

Linux命令行与Shell脚本编程 第一章 文章目录 Linux命令行与Shell脚本编程六.函数6.1.脚本函数基础6.1.1.创建函数6.1.2.使用函数 6.2.函数返回值6.2.1.默认的退出状态码6.2.2.使用return命令6.2.3.使用函数输出 6.3.函数中使用变量6.3.1.向函数传递参数6.3.2.在函数中处理变量…...

SpringCloud-Hystrix服务熔断与降级工作原理源码 | 京东物流技术团队

先附上Hystrix源码图 在微服务架构中&#xff0c;根据业务来拆分成一个个的服务&#xff0c;服务与服务之间可以相互调用&#xff08;RPC&#xff09;&#xff0c;在Spring Cloud可以用RestTemplateRibbon和Feign来调用。为了保证其高可用&#xff0c;单个服务通常会集群部署。…...

(一)react脚手架

1. react脚手架 react提供了一个用于创建react项目的脚手架库&#xff1a;create-react-app 项目的整体技术架构为&#xff1a;react webpack es6 eslint 使用脚手架开发的项目的特点&#xff1a;模块化、组件化、工程化 2. 创建项目并启动 # 第一步&#xff1a; 全局安…...

Typescript中的元组与数组的区别

Typescript中的元组与数组的区别 元组可以应用在经纬度这样明确固定长度和类型的场景下 //元组和数组类似&#xff0c;但是类型注解时会不一样//元组赋值的类型、位置、个数需要和定义的类型、位置、个数完全一致&#xff0c;不然会报错。 // 数组 某个位置的值可以是注解中的…...