Head First设计模式--工厂模式

工厂模式

所有工厂都是用来封装对象创建,减少应用程序与具体类的依赖

#简单工厂(静态工厂)

  • 意图:我们把创建比萨的代码包装进一个类,当以后实现改变时,只需修改这个类就可以了。我们也要把具体实例化的过程,从客户的代码中删除!

在这里插入图片描述

	public class SimplePizzaFactory {
		public Pizza createPizza(String type) {
			Pizza pizza = null;
			if (type.equals("cheese")) {
				pizza = new CheesePizza();
			} else if (type.equals("pepperoni")) {
				pizza = new PepperoniPizza();
			} else if (type.equals("clam")) {
				pizza = new ClamPizza();
			} else if (type.equals("veggie")) {
				pizza = new VeggiePizza();
			}
			return pizza;
		}
	}

#工厂方法
定义了一个创建对象的接口,但有子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类

在这里插入图片描述

	//更改PizzaStore为抽象类
	public abstract class PizzaStore {
		public Pizza orderPizza(String type) {
			Pizza pizza;
			// 现在createPizza()方法从工厂对象中移回PizzaStore
			pizza = createPizza(type);
			pizza.prepare();
			pizza.bake();
			pizza.cut();
			pizza.box();
			return pizza;
		}

		// 在PizzaStore里,"工厂方法"现在是抽象的
		// 现在把工厂对象移动这个方法中
		protected abstract Pizza createPizza(String type);
	}
	public class NYPizzaStore extends PizzaStore {
		// 该方法返回一个Pizza对象,由子类全权负责该实例化哪一个具体Pizza
		@Override
		protected Pizza createPizza(String type) {
			// 创建具体类的地方。对于每一种比萨类型,都是创建纽约风味

			if (type.equals("cheese")) {
				return new NYStyleCheesePizza();
			} else if (type.equals("veggie")) {
				return new NYStyleVeggiePizza();
			}
			return null;
		}
		// 注意,超累的orderPizza()方法,并不知道正在创建的比萨是哪一种,它只知道这个比萨可以被准备,被烘烤,被切片,被装盒!
	}
	// 从一个抽象比萨类开始,所有的具体比萨都必须派生自这个类
	public abstract class Pizza {
		// 每个比萨都具有名称,面团类型,酱料类型,一套佐料
		String name;
		String dough;
		String sauce;
		ArrayList toppings = new ArrayList();
		void prepare() {
			System.out.println("Preparing" + name);
		}
		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 class NYStyleCheesePizza extends Pizza {
		public NYStyleCheesePizza() {
			// 纽约比萨有自己的大蒜番茄酱和博饼
			name = "NY Style Sauce and Cheese Pizza";
			dough = "Thin Crust Dough";
			sauce = "Marinara Sauce";
		}
	}
	public class Test {
			public static void main(String[] args) {
			PizzaStore nyStore = new NYPizzaStore();
			Pizza pizza = nyStore.orderPizza("cheese");
			System.out.println("Ethan ordered a " + pizza.getName() + "\n");
		}
		
	}

#抽象工厂(原料工厂)
用于创建相关对象的家族,而不需要依赖具体类
在这里插入图片描述

	public interface IngredientFactory {
		public Dough createDough();
		public Cheese createCheese();
	}
	public class NYPizzaIngredientFactory implements IngredientFactory {
		@Override
		public Dough createDough() {
			return new ThickDough();
		}
		@Override
		public Cheese createCheese() {
			return new PepperCheese();
		}
	}
	public class CheesePizza extends Pizza {
		PizzaIngredientFactory ingredientFactory;
		public CheesePizza(PizzaIngredientFactory ingredientFacotry){
		this.ingredientFactory =ingredientFacotry;
		}
		@Override
		void prepare() {
			System.out.println("Preparing " + name);
			dough = ingredientFactory.createDough();
			cheese = ingredientFactory.createCheese();
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值