举例制造Pizza的工厂
简单工厂模式(一般用这种就可以了)
工厂类里, 通过 if else 来创建不同的具体类. 多个品牌只有一个工厂,通过 if else 来判断制造什么样的Pizza(海鲜Pizza, 榴莲Pizza)
package com.leon.design; public class SimpleFactory { public static void main(String[] args) { PizzaFactory factory = new PizzaFactory(); Pizza p1 = factory.CreatePizza("Cheese"); Pizza p2 = factory.CreatePizza("Greek"); p1.prepare(); p1.bake(); p2.prepare(); p2.bake(); } } abstract class Pizza { protected String name; //抽象方法, 因为不同的pizza, 准备的方法不一样, 所以需要继承自己去实现 public abstract void prepare(); public void bake() { System.out.println(name + " fire baking"); } public void cut() { System.out.println(name + " knife cutting"); } public void box() { System.out.println(name + " people boxing"); } public void setName(String name) { this.name = name; } } class CheesePizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put cheese into pizza"); } } class GreekPizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put greek into pizza"); } } class PizzaFactory { public Pizza CreatePizza(String s) { if (s.equals("Cheese")) { return new CheesePizza(); } else if (s.equals("Greek")) { return new GreekPizza(); } return null; } }
工厂模式: 每个品牌有自己的制造工厂
package com.leon.design; public class StandardFactory { public static void main(String[] args) { CheesePizzaFactory cheeseFactory = new CheesePizzaFactory(); GreekPizzaFactory greekFactory = new GreekPizzaFactory(); Pizza p1 = cheeseFactory.CreatePizza(); Pizza p2 = greekFactory.CreatePizza(); p1.prepare(); p1.bake(); p2.prepare(); p2.bake(); } } abstract class Pizza { protected String name; //抽象方法, 因为不同的pizza, 准备的方法不一样, 所以需要继承自己去实现 public abstract void prepare(); public void bake() { System.out.println(name + " fire baking"); } public void cut() { System.out.println(name + " knife cutting"); } public void box() { System.out.println(name + " people boxing"); } public void setName(String name) { this.name = name; } } class CheesePizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put cheese into pizza"); } } class GreekPizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put greek into pizza"); } } class CheesePizzaFactory { public Pizza CreatePizza() { return new CheesePizza(); } } class GreekPizzaFactory { public Pizza CreatePizza() { return new GreekPizza(); } }
抽象工厂模式
定义了一个interface 用于创建相关或有依赖关系的对象族, 而无需指明具体的类.
简单工厂 + 工厂方法 的结合, 抽象工厂 + 具体实现的工厂子类
把工厂和产品都要做一个抽象.
package com.leon.design; public class AbstractFactory { public static void main(String[] args) { IPizzaFactory factory1 = new CheesePizzaFactory(); IPizzaFactory factory2 = new GreekPizzaFactory(); Pizza p1 = factory1.createPizza(); Pizza p2 = factory2.createPizza(); p1.prepare(); p1.bake(); p2.prepare(); p2.bake(); } } abstract class Pizza { protected String name; //抽象方法, 因为不同的pizza, 准备的方法不一样, 所以需要继承自己去实现 public abstract void prepare(); public void bake() { System.out.println(name + " fire baking"); } public void cut() { System.out.println(name + " knife cutting"); } public void box() { System.out.println(name + " people boxing"); } public void setName(String name) { this.name = name; } } class CheesePizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put cheese into pizza"); } } class GreekPizza extends Pizza { @Override public void prepare() { System.out.println(name + " preapare: put greek into pizza"); } } interface IPizzaFactory{ public Pizza createPizza(); } class CheesePizzaFactory implements IPizzaFactory { @Override public Pizza createPizza() { return new CheesePizza(); } } class GreekPizzaFactory implements IPizzaFactory { @Override public Pizza createPizza() { return new GreekPizza(); } }