| 1) 简单工厂模式是属于创建型模式,是工厂模式的一种。简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式 |
| 2) 简单工厂模式:定义了一个创建对象的类,由这个类来封装实例化对象的行为(代码) |
| 3) 在软件开发中,当我们会用到大量的创建某种、某类或者某批对象时,就会使用到工厂模式 |

| package com.atguigu.factory.simplefactory.pizzastore.pizza; |
| |
| 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; |
| } |
| } |
| |
| public class PepperPizza extends Pizza { |
| @Override |
| public void prepare() { |
| |
| System.out.println(" 给胡椒披萨准备原材料 "); |
| } |
| } |
| |
| public class GreekPizza extends Pizza { |
| @Override |
| public void prepare() { |
| |
| System.out.println(" 给希腊披萨 准备原材料 "); |
| } |
| } |
| |
| public class CheesePizza extends Pizza { |
| @Override |
| public void prepare() { |
| |
| System.out.println(" 给制作奶酪披萨 准备原材料 "); |
| } |
| } |
| |
| package com.atguigu.factory.simplefactory.pizzastore.order; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.CheesePizza; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.GreekPizza; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.PepperPizza; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza; |
| |
| public class SimpleFactory { |
| |
| |
| public Pizza createPizza(String orderType) { |
| |
| Pizza pizza = null; |
| |
| System.out.println("使用简单工厂模式"); |
| if (orderType.equals("greek")) { |
| pizza = new GreekPizza(); |
| pizza.setName(" 希腊披萨 "); |
| } else if (orderType.equals("cheese")) { |
| pizza = new CheesePizza(); |
| pizza.setName(" 奶酪披萨 "); |
| } else if (orderType.equals("pepper")) { |
| pizza = new PepperPizza(); |
| pizza.setName("胡椒披萨"); |
| } |
| return pizza; |
| } |
| } |
| |
| package com.atguigu.factory.simplefactory.pizzastore.order; |
| import java.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStreamReader; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza; |
| public class OrderPizza { |
| |
| |
| SimpleFactory simpleFactory; |
| Pizza pizza = null; |
| |
| |
| public OrderPizza(SimpleFactory simpleFactory) { |
| setFactory(simpleFactory); |
| } |
| |
| public void setFactory(SimpleFactory simpleFactory) { |
| String orderType = ""; |
| |
| this.simpleFactory = simpleFactory; |
| |
| do { |
| orderType = getType(); |
| pizza = this.simpleFactory.createPizza(orderType); |
| |
| |
| if(pizza != null) { |
| pizza.prepare(); |
| pizza.bake(); |
| pizza.cut(); |
| pizza.box(); |
| } else { |
| System.out.println(" 订购披萨失败 "); |
| break; |
| } |
| }while(true); |
| } |
| |
| |
| private String getType() { |
| try { |
| BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); |
| System.out.println("input pizza 种类:"); |
| String str = strin.readLine(); |
| return str; |
| } catch (IOException e) { |
| e.printStackTrace(); |
| return ""; |
| } |
| } |
| |
| } |
| |
| package com.atguigu.factory.simplefactory.pizzastore.order; |
| |
| |
| public class PizzaStore { |
| public static void main(String[] args) { |
| |
| |
| new OrderPizza(new SimpleFactory()); |
| System.out.println("~~退出程序~~"); |
| } |
| } |
| public class SimpleFactory { |
| |
| |
| public static Pizza createPizza2(String orderType) { |
| |
| Pizza pizza = null; |
| |
| System.out.println("使用简单工厂模式2"); |
| if (orderType.equals("greek")) { |
| pizza = new GreekPizza(); |
| pizza.setName(" 希腊披萨 "); |
| } else if (orderType.equals("cheese")) { |
| pizza = new CheesePizza(); |
| pizza.setName(" 奶酪披萨 "); |
| } else if (orderType.equals("pepper")) { |
| pizza = new PepperPizza(); |
| pizza.setName("胡椒披萨"); |
| } |
| return pizza; |
| } |
| |
| } |
| |
| package com.atguigu.factory.simplefactory.pizzastore.order; |
| import java.io.BufferedReader; |
| import java.io.IOException; |
| import java.io.InputStreamReader; |
| import com.atguigu.factory.simplefactory.pizzastore.pizza.Pizza; |
| public class OrderPizza2 { |
| |
| Pizza pizza = null; |
| String orderType = ""; |
| |
| public OrderPizza2() { |
| |
| do { |
| orderType = getType(); |
| pizza = SimpleFactory.createPizza2(orderType); |
| |
| |
| if (pizza != null) { |
| pizza.prepare(); |
| pizza.bake(); |
| pizza.cut(); |
| pizza.box(); |
| } else { |
| System.out.println(" 订购披萨失败 "); |
| break; |
| } |
| } while (true); |
| } |
| |
| |
| private String getType() { |
| try { |
| BufferedReader strin = new BufferedReader(new InputStreamReader(System.in)); |
| System.out.println("input pizza 种类:"); |
| String str = strin.readLine(); |
| return str; |
| } catch (IOException e) { |
| e.printStackTrace(); |
| return ""; |
| } |
| } |
| } |
| |
| package com.atguigu.factory.simplefactory.pizzastore.order; |
| |
| public class PizzaStore { |
| public static void main(String[] args) { |
| |
| new OrderPizza2(); |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术