实现了抽象产品角色所定义的接口,由具体工厂来创建,它 同具体工厂之间是多对一的关系。
三、优缺点
可以在类的内部对产品族中相关联的多等级产品共同管理,而不必专门引入多个新的类进行管理;
当增加一个新的产品族时不需要修改原代码,满足开闭原则。
当产品族中需要增加一个新的产品时,所有的工厂类都需要进行修改。
四、《Head First 设计模式》读后感
1、抽象工厂模式提供了一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
2、工厂方法模式运用的是继承,抽象工厂模式运用的是对象的组合。
五、JDK源码分析
package com.guor.jdk;
import java.util.Calendar;
public class Factory {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
// 注意月份下标从0开始,所以取月份要+1
System.out.println(“年:” + cal.get(Calendar.YEAR));
System.out.println(“月:” + (cal.get(Calendar.MONTH) + 1));
System.out.println(“日:” + cal.get(Calendar.DAY_OF_MONTH));
System.out.println(“时:” + cal.get(Calendar.HOUR_OF_DAY));
System.out.println(“分:” + cal.get(Calendar.MINUTE));
System.out.println(“秒:” + cal.get(Calendar.SECOND));
}
}
六、代码实例
(1)order包包
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.Pizza;
//一个抽象工厂模式的抽象层(接口)
public interface AbsFactory {
//让下面的工厂子类来 具体实现
public Pizza createPizza(String orderType);
}
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.BJCheesePizza;
import com.guor.abstractFactory.pizza.BJPepperPizza;
import com.guor.abstractFactory.pizza.Pizza;
//这是工厂子类
public class BJFactory implements AbsFactory {
@Override
public Pizza createPizza(String orderType) {
System.out.println(“使用的是抽象工厂模式”);
Pizza pizza = null;
if(orderType.equals(“cheese”)) {
pizza = new BJCheesePizza();
} else if (orderType.equals(“pepper”)){
pizza = new BJPepperPizza();
}
return pizza;
}
}
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.LDCheesePizza;
import com.guor.abstractFactory.pizza.LDPepperPizza;
import com.guor.abstractFactory.pizza.Pizza;
public class LDFactory implements AbsFactory {
@Override
public Pizza createPizza(String orderType) {
System.out.println(“使用的是抽象工厂模式”);
Pizza pizza = null;
if (orderType.equals(“cheese”)) {
pizza = new LDCheesePizza();
} else if (orderType.equals(“pepper”)) {
pizza = new LDPepperPizza();
}
return pizza;
}
}
package com.guor.abstractFactory.order;
import com.guor.abstractFactory.pizza.Pizza;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrderPizza {
AbsFactory factory;
// 构造器
public OrderPizza(AbsFactory factory) {
setFactory(factory);
}
private void setFactory(AbsFactory factory) {
Pizza pizza = null;
String orderType = “”; // 用户输入
this.factory = factory;
do {
orderType = getType();
// factory 可能是北京的工厂子类,也可能是伦敦的工厂子类
pizza = factory.createPizza(orderType);
if (pizza != null) { // 订购ok
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
} else {
System.out.println(“订购失败”);
break;
}
《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》
【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享
} 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.guor.abstractFactory.order;
public class PizzaStore {
public static void main(String[] args) {
new OrderPizza(new BJFactory());
}
}
(2)pizza包包
package com.guor.abstractFactory.pizza;
public class BJCheesePizza extends Pizza {
@Override
public void prepare() {
setName(“北京的奶酪pizza”);
System.out.println(“北京的奶酪pizza 准备原材料”);
}
}
package com.guor.abstractFactory.pizza;
public class BJPepperPizza extends Pizza {
@Override
public void prepare() {