//抽象工厂模式原型
//提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。如CPU和主板间是有关系的,所以需要创建一系列对象接口。
public interface AbstractFactory {
public AbstractProductA createProductA();
public AbstractProductB createProductB();
}
public interface AbstractProductA {}
public interface AbstractProductB {}
public class ProductA1 implements AbstractProductA {}
public class ProductA2 implements AbstractProductA {}
public class ProductB1 implements AbstractProductB {}
public class ProductB2 implements AbstractProductB {}
public class ConcreteFactory1 implements AbstractFactory {
public AbstractProductA createProductA {
return new ProductA1();
}
public AbstractProductB createProductB {
return new ProductB1();
}
}
public class ConcreteFactory2 implements AbstractFactory {
public AbstractProductA createProductA {
return new ProductA2();
}
public AbstractProductB createProductB {
return new ProductB2();
}
}
public class Client {
public static void main(String[] args) {
AbstractFactory af = new ConcreteFactory1();
af.createProductA();
af.createProductB();
}
}
=====================================================================
//抽象工厂模式具体实例
public interface AbstractFactory {
public CPUApi createCUPApi();
public MainboardApi createMainboardApi();
}
//装机方案1
public class Schema1 implements AbstractFactory {
public CPUApi createCPUApi() {
return new IntelCPU(1156);
}
public MainBoardApi createMainboardApi() {
return new GAMainboard(1156);
}
}
//装机方案2
public class Schema2 implements AbstractFactory {
public CPUApi createCPUApi() {
return new AMDCPU(939);
}
public MainBoardApi createMainboardApi() {
return new MSIMainboard(939);
}
}
public class ComputerEngineer {
private CPUApi cpu = null;
private MainboardApi mainboard = null;
public void makeCoputer(AbstractFactory schema) {
prepareHardwares(schema);
}
private void prepareHardwares(AbstractFactory schema) {
this.cpu = schema.createCPUApi();
this.mainboard = schema.createMainboardApi();
this.cpu.calculate();//测试
this.mainboard.installCPU();//测试
}
}
//这种抽象工厂模式有个问题,如果新增了内存组件怎么办,需要对抽象工厂类和每个实现类都进行修改,不够方便
======================================================================================================
//以下是一种改进的抽象工厂模式,但不够安全,叫做可扩展的工厂
public interface AbstractFactory {
//这个工厂里,type用来指定创建什么类型的产品(CPU,主板,内存等),而不管具体创建什么型号的CPU,主板,内存
public Object createProduct(int type);
}
//装机方案1
public class Schema1 implements AbstractFactory {
public Object createProduct(int type) {
Object retObj = null;
if(type == 1) {
retObj = new IntelCPU(1156);
}else if(type == 2) {
retObj = new GAMainboard(1156);
}
return retObj;
//一次仅创建一个特定方案的产品
}
}
public class Schema2 implements AbstractFactory {
public Object createProduct(int type) {
Object retObj = null;
if(type == 1) {
retObj = new AMDCPU(939);
}else if(type == 2) {
retObj = new MSIMainboard(939);
}
return retObj;
//一次仅创建一个特定方案的产品
}
}
public class ComputerEngineer {
private CPUApi cpu = null;
private MainboardApi mainboard = null;
public void makeCoputer(AbstractFactory schema) {
prepareHardwares(schema);
}
private void prepareHardwares(AbstractFactory schema) {
this.cpu = (CPUApi)schema.createProduct(1);//+++
this.mainboard = (MainboardApi)schema.createMainboardApi();//+++
this.cpu.calculate();//测试
this.mainboard.installCPU();//测试
}
}
//不安全的原因:因为创建产品对象后,需要转型为具体的对象,因为返回的是Object,如果这个时候没有匹配上,比如返回的不是CPU对象,但是要强制造型成为CPU,那么就会发生错误,因此这种实现方式的一个潜在缺点是不太安全
//体会下灵活性
public interface MemoryApi {
public void cacheData();
}
public class HyMemory implements MemoryApi {
public void cacheData() {
//内存方法
}
}
//装机方案3
public class Schema3 implements AbstractFactory {
public Object createProduct(int type) {
Object retObj = null;
if(type == 1) {
retObj = new AMDCPU(1156);
}else if(type == 2) {
retObj = new MSIMainboard(1156);
}else if(type == 3) {
retObj = new HyMemory();//+++
}
return retObj;
//一次仅创建一个特定方案的产品
}
}
public class ComputerEngineer {
private CPUApi cpu = null;
private MainboardApi mainboard = null;
private MemoryApi memory = null;
public void makeCoputer(AbstractFactory schema) {
prepareHardwares(schema);
}
private void prepareHardwares(AbstractFactory schema) {
this.cpu = (CPUApi)schema.createProduct(1);//+++
this.mainboard = (MainboardApi)schema.createMainboardApi();//+++
this.memory = (MemoryApi)schema.createProduct(3);
this.cpu.calculate();//测试
this.mainboard.installCPU();//测试
if(memory != null) {//同时满足了之前和现在的需求
this.memory.cacheData();//+++
}
}
}
设计模式入门之抽象工厂模式
最新推荐文章于 2024-06-03 18:37:21 发布