文章目录
- 1.引出工厂模式
- 具体需求
- 2.传统模式
- 1.类图
- 2.目录结构
- 3.pizzastore 用于设计pizza
- 1.Pizza.java 抽象的Pizza类型
- 2.CheesePizaa.java CheesePizaa
- 3.GreekPizza.java GreekPizza
- 4.order 用于订购和制作pizza
- 1.OrderPizza.java 制作pizza
- 2.PizzaStore.java 订购pizza
- 5.优缺点分析
- 3.简单工厂模式
- 1.基本介绍
- 2.类图
- 3.目录结构
- 4.orderbysimplefactory
- 1.SimpleFactory.java 简单工厂,根据类型来创建对象
- 2.OrderPizzaBySimpleFactory.java 聚合一个工厂,来得到对象
- 3.PizzaStoreBySimpleFactory.java 订购pizza
- 5.pizzastore 用于设计pizza跟上面的一样
- 6.简单工厂模式的小结
- 4.工厂方法模式
- 1.具体需求引入工厂方法模式
- 2.基本介绍
- 3.类图
- 4.目录结构
- 5.pizzastore 设置pizza
- 1.Pizza.java 抽象的Pizza类型
- 2.BJCheesePizza.java
- 3.BJPepperPizza.java
- 4.LDCheesePizza.java
- 5.LDPepperPizza.java
- 6.order 制作pizza
- 1.OrderPizza.java 制作Pizza的抽象类
- 2.BJOrderPizza.java 根据不同的类型创建不同类型的BJpizza
- 3.LDOrderPizza.java 根据不同的类型创建不同类型的LDpizza
- 4.PizzaStore.java 订购pizza
- 7.工厂方法模式小结
- 5.抽象工厂模式
- 1.基本介绍
- 2.类图
- 3.目录结构
- 4.order
- 1.AbsFactory.java 抽象工厂
- 2.BJFactory.java 北京的具体工厂
- 3.LDFactory.java 伦敦的具体工厂
- 4.OrderPizza.java 根据不同的具体工厂,和类型来制造对应的pizza
- 5.PizzaStore.java 订购pizza
- 6.JDK源码分析
- 1.Calendar类使用了简单工厂模式
- 2.工厂模式小结
1.引出工厂模式
具体需求
2.传统模式
1.类图
2.目录结构
3.pizzastore 用于设计pizza
1.Pizza.java 抽象的Pizza类型
package com.sun.simplefactory.pizzastore;/*** Description: 抽象的Pizza类型* @Author sun* @Create 2024/5/27 15:02* @Version 1.0*/
public abstract class Pizza {protected String name;public abstract void prepare();public void bake() {System.out.println(name + " baking;");}public void cut() {System.out.println(name + " cutting;");}public void box() {System.out.println(name + " boxing;");}public void setName(String name) {this.name = name;}
}
2.CheesePizaa.java CheesePizaa
package com.sun.simplefactory.pizzastore;/*** Description: CheesePizaa* @Author sun* @Create 2024/5/27 15:07* @Version 1.0*/
public class CheesePizaa extends Pizza {@Overridepublic void prepare() {System.out.println("CheesePizaa preparing;");}
}
3.GreekPizza.java GreekPizza
package com.sun.simplefactory.pizzastore;/*** Description: GreekPizza* @Author sun* @Create 2024/5/27 15:07* @Version 1.0*/
public class GreekPizza extends Pizza{@Overridepublic void prepare() {System.out.println("GreekPizza preparing;");}
}
4.order 用于订购和制作pizza
1.OrderPizza.java 制作pizza
package com.sun.simplefactory.order;import com.sun.simplefactory.pizzastore.CheesePizaa;
import com.sun.simplefactory.pizzastore.GreekPizza;
import com.sun.simplefactory.pizzastore.Pizza;/*** Description: 制作pizza* @Author sun* @Create 2024/5/27 15:08* @Version 1.0*/
public class OrderPizza {// 根据用户的输入,返回对应的Pizza对象,获取用户的输入public void createPizza(String orderType) {Pizza pizza = null;if (orderType.equals("greek")) {pizza = new GreekPizza();pizza.setName("GreekPizza");} else if (orderType.equals("cheese")) {pizza = new CheesePizaa();pizza.setName("CheesePizaa");}// 输出pizza制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();}
}
2.PizzaStore.java 订购pizza
package com.sun.simplefactory.order;/*** Description: 订购pizza* @Author sun* @Create 2024/5/27 15:50* @Version 1.0*/
public class PizzaStore {public static void main(String[] args) {// 订购pizza,相当于客户端new OrderPizza().createPizza("greek");}
}
5.优缺点分析
3.简单工厂模式
1.基本介绍
2.类图
3.目录结构
4.orderbysimplefactory
1.SimpleFactory.java 简单工厂,根据类型来创建对象
package com.sun.simplefactory.orderbysimplefactory;import com.sun.simplefactory.pizzastore.CheesePizaa;
import com.sun.simplefactory.pizzastore.GreekPizza;
import com.sun.simplefactory.pizzastore.Pizza;/*** Description: 简单工厂模式* @Author sun* @Create 2024/5/27 16:18* @Version 1.0*/
public class SimpleFactory {Pizza pizza = null;public Pizza createPizza(String orderType) {System.out.println("使用简单工厂模式");// 根据用户输入的类型来获取Pizza对象if (orderType.equals("greek")) {pizza = new GreekPizza();pizza.setName("GreekPizza");} else if (orderType.equals("cheese")) {pizza = new CheesePizaa();pizza.setName("CheesePizaa");}return pizza;}
}
2.OrderPizzaBySimpleFactory.java 聚合一个工厂,来得到对象
package com.sun.simplefactory.orderbysimplefactory;import com.sun.simplefactory.pizzastore.Pizza;/*** Description: 通过简单工厂来订购pizza* @Author sun* @Create 2024/5/27 16:39* @Version 1.0*/
public class OrderPizzaBySimpleFactory {// 定义一个简单工厂的属性private SimpleFactory simpleFactory;// 使用构造器创建一个对象并注入工厂public OrderPizzaBySimpleFactory(SimpleFactory simpleFactory) {setSimpleFactory(simpleFactory);}// 使用set方法聚合一个pizza的工厂public void setSimpleFactory(SimpleFactory simpleFactory) {this.simpleFactory = simpleFactory;// 记录用户输入的pizza类型String orderType = "greek";// 根据用户输入的类型,从工厂中获取一个该类型的pizzaPizza pizza = simpleFactory.createPizza(orderType);// 判断是否获取到了指定类型的pizzaif (pizza != null) {// 输出pizza制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();} else {System.out.println("订购pizza失败!");}}
}
3.PizzaStoreBySimpleFactory.java 订购pizza
package com.sun.simplefactory.orderbysimplefactory;/*** Description:* @Author sun* @Create 2024/5/27 15:50* @Version 1.0*/
public class PizzaStoreBySimpleFactory {public static void main(String[] args) {// 订购pizza,相当于客户端new OrderPizzaBySimpleFactory(new SimpleFactory());}
}
5.pizzastore 用于设计pizza跟上面的一样
6.简单工厂模式的小结
需要根据类型的不同来创建不同的对象时,可将其做成一个简单工厂
4.工厂方法模式
1.具体需求引入工厂方法模式
2.基本介绍
3.类图
4.目录结构
5.pizzastore 设置pizza
1.Pizza.java 抽象的Pizza类型
package com.sun.factorymethod.pizzastore;/*** Description: 抽象的Pizza类型* @Author sun* @Create 2024/5/27 15:02* @Version 1.0*/
public abstract class Pizza {protected String name;public abstract void prepare();public void bake() {System.out.println(name + " baking;");}public void cut() {System.out.println(name + " cutting;");}public void box() {System.out.println(name + " boxing;");}public void setName(String name) {this.name = name;}
}
2.BJCheesePizza.java
package com.sun.factorymethod.pizzastore;/*** Description:* @Author sun* @Create 2024/5/27 19:13* @Version 1.0*/
public class BJCheesePizza extends Pizza{@Overridepublic void prepare() {super.setName("北京的奶酪pizza");System.out.println("北京的奶酪pizza 准备原材料");}
}
3.BJPepperPizza.java
package com.sun.factorymethod.pizzastore;/*** Description:* @Author sun* @Create 2024/5/27 19:16* @Version 1.0*/
public class BJPepperPizza extends Pizza{@Overridepublic void prepare() {super.setName("北京的胡椒pizza");System.out.println("北京的胡椒pizza 准备原材料");}
}
4.LDCheesePizza.java
package com.sun.factorymethod.pizzastore;/*** Description:* @Author sun* @Create 2024/5/27 19:13* @Version 1.0*/
public class LDCheesePizza extends Pizza{@Overridepublic void prepare() {super.setName("伦敦的奶酪pizza");System.out.println("伦敦的奶酪pizza 准备原材料");}
}
5.LDPepperPizza.java
package com.sun.factorymethod.pizzastore;/*** Description:* @Author sun* @Create 2024/5/27 19:16* @Version 1.0*/
public class LDPepperPizza extends Pizza{@Overridepublic void prepare() {super.setName("伦敦的胡椒pizza");System.out.println("伦敦的胡椒pizza 准备原材料");}
}
6.order 制作pizza
1.OrderPizza.java 制作Pizza的抽象类
package com.sun.factorymethod.order;import com.sun.factorymethod.pizzastore.Pizza;/*** Description: 制作Pizza的抽象类* @Author sun* @Create 2024/5/27 19:21* @Version 1.0*/
public abstract class OrderPizza {abstract Pizza createPizza(String orderType);// 构造方法,当子类创建对象的时候就会调用public OrderPizza() {Pizza pizza = null;// 订单的类型,子类根据订单的类型来创建订单String orderType = "cheese";pizza = this.createPizza(orderType);// 输出pizza的制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();}
}
2.BJOrderPizza.java 根据不同的类型创建不同类型的BJpizza
package com.sun.factorymethod.order;import com.sun.factorymethod.pizzastore.BJCheesePizza;
import com.sun.factorymethod.pizzastore.BJPepperPizza;
import com.sun.factorymethod.pizzastore.Pizza;/*** Description:* @Author sun* @Create 2024/5/27 19:35* @Version 1.0*/
public class BJOrderPizza extends OrderPizza{@OverridePizza createPizza(String orderType) {// 根据不同的类型创建不同类型的pizzaPizza pizza = null;if ("cheese".equals(orderType)) {pizza = new BJCheesePizza();} else if ("pepper".equals(orderType)) {pizza = new BJPepperPizza();}return pizza;}
}
3.LDOrderPizza.java 根据不同的类型创建不同类型的LDpizza
package com.sun.factorymethod.order;import com.sun.factorymethod.pizzastore.*;/*** Description:* @Author sun* @Create 2024/5/27 19:35* @Version 1.0*/
public class LDOrderPizza extends OrderPizza{@OverridePizza createPizza(String orderType) {// 根据不同的类型创建不同类型的pizzaPizza pizza = null;if ("cheese".equals(orderType)) {pizza = new LDCheesePizza();} else if ("pepper".equals(orderType)) {pizza = new LDPepperPizza();}return pizza;}
}
4.PizzaStore.java 订购pizza
package com.sun.factorymethod.order;/*** Description:* @Author sun* @Create 2024/5/27 19:48* @Version 1.0*/
public class PizzaStore {public static void main(String[] args) {// 创建北京口味的pizzaBJOrderPizza bjOrderPizza = new BJOrderPizza();// 创建伦敦口味的pizzaLDOrderPizza ldOrderPizza = new LDOrderPizza();}
}
7.工厂方法模式小结
每个方法都相当于一个工厂,能够根据不同类型创建不同的对象,这里还结合了模板方法模式,当子类创建对象,会调用父类的构造器,而构造器内根据类型调用子类创建pizza的方法
5.抽象工厂模式
1.基本介绍
2.类图
3.目录结构
4.order
1.AbsFactory.java 抽象工厂
package com.sun.absfactory.order;import com.sun.factorymethod.pizzastore.Pizza;/*** Description: 抽象工厂* @Author sun* @Create 2024/5/27 20:20* @Version 1.0*/
public interface AbsFactory {Pizza createPizza(String orderType);
}
2.BJFactory.java 北京的具体工厂
package com.sun.absfactory.order;import com.sun.factorymethod.pizzastore.*;/*** Description:* @Author sun* @Create 2024/5/27 20:22* @Version 1.0*/
public class BJFactory implements AbsFactory{@Overridepublic Pizza createPizza(String orderType) {// 根据不同的类型创建不同类型的BJpizzaPizza pizza = null;if ("cheese".equals(orderType)) {pizza = new BJCheesePizza();} else if ("pepper".equals(orderType)) {pizza = new BJPepperPizza();}return pizza;}
}
3.LDFactory.java 伦敦的具体工厂
package com.sun.absfactory.order;import com.sun.factorymethod.pizzastore.LDCheesePizza;
import com.sun.factorymethod.pizzastore.LDPepperPizza;
import com.sun.factorymethod.pizzastore.Pizza;/*** Description:* @Author sun* @Create 2024/5/27 20:22* @Version 1.0*/
public class LDFactory implements AbsFactory {@Overridepublic Pizza createPizza(String orderType) {// 根据不同的类型创建不同类型的LDpizzaPizza pizza = null;if ("cheese".equals(orderType)) {pizza = new LDCheesePizza();} else if ("pepper".equals(orderType)) {pizza = new LDPepperPizza();}return pizza;}
}
4.OrderPizza.java 根据不同的具体工厂,和类型来制造对应的pizza
package com.sun.absfactory.order;import com.sun.factorymethod.pizzastore.Pizza;/*** Description:* @Author sun* @Create 2024/5/27 20:26* @Version 1.0*/
public class OrderPizza {// 聚合一个抽象工厂private AbsFactory absFactory;// 通过构造器再聚合一下public OrderPizza(AbsFactory absFactory) {this.setAbsFactory(absFactory);}public void setAbsFactory(AbsFactory absFactory) {this.absFactory = absFactory;Pizza pizza = null;// 订单的类型,子类根据订单的类型来创建订单String orderType = "cheese";pizza = absFactory.createPizza(orderType);// 输出pizza的制作过程pizza.prepare();pizza.bake();pizza.cut();pizza.box();}
}
5.PizzaStore.java 订购pizza
package com.sun.absfactory.order;/*** Description:* @Author sun* @Create 2024/5/27 20:39* @Version 1.0*/
public class PizzaStore {public static void main(String[] args) {// 使用伦敦的工厂,来订购pizaaOrderPizza orderPizza = new OrderPizza(new LDFactory());}
}