GoF 创建型 抽象工厂模式

抽象工厂模式的定义

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

抽象工厂模式的本质

选择产品簇的实现。

抽象工厂模式的优缺点

优点
 分离接口和实现。
 使得切换产品簇变得容易。
缺点
 容易造成类层次结构复杂

示例1

public interface CpuApi {
    void calulate();
}
public class AmdCpu implements CpuApi {
    @Override
    public void calulate() {
        System.out.println("AMD CPU");
    }
}
public class IntelCpu implements CpuApi {
    @Override
    public void calulate() {
        System.out.println("Intel CPU");
    }
}
public interface MainboardApi {
    void installCpu();
}
public class MSIMainboard implements MainboardApi {
    @Override
    public void installCpu() {
        System.out.println("微星主板");
    }
}
public class GAMainboard implements MainboardApi{
    @Override
    public void installCpu() {
        System.out.println("技嘉主板");
    }
}
public interface AbstractFactory {

    CpuApi createCpuApi();

    MainboardApi createMainboardApi();
}
import org.baozi.afactory.cpu.AmdCpu;
public class Schema1 implements AbstractFactory {

    @Override
    public CpuApi createCpuApi() {
        return new AmdCpu();
    }

    @Override
    public MainboardApi createMainboardApi() {
        return new MSIMainboard();
    }
}
public class Schema2 implements AbstractFactory{

    @Override
    public CpuApi createCpuApi() {
        return new IntelCpu();
    }

    @Override
    public MainboardApi createMainboardApi() {
        return new GAMainboard();
    }
}
public class ComputerEngineer {

    void makeComputer(AbstractFactory schema) {
        schema.createMainboardApi().installCpu();
        schema.createCpuApi().calulate();
    }
}
public class TestClient {
    public static void main(String[] args) {
        ComputerEngineer engineer = new ComputerEngineer();
        engineer.makeComputer(new Schema1());
    }
}

示例2 参数式

public interface CpuApi {
    void calulate();
}
public class AmdCpu implements CpuApi {
    @Override
    public void calulate() {
        System.out.println("AMD CPU");
    }
}
public class IntelCpu implements CpuApi {
    @Override
    public void calulate() {
        System.out.println("Intel CPU");
    }
}
public interface MainboardApi {
    void installCpu();
}
public class MSIMainboard implements MainboardApi {
    @Override
    public void installCpu() {
        System.out.println("微星主板");
    }
}
public class GAMainboard implements MainboardApi{
    @Override
    public void installCpu() {
        System.out.println("技嘉主板");
    }
}
public interface AbstractFactory {
    Object createProduct(int type);
}
public class Schema1 implements AbstractFactory {

    @Override
    public Object createProduct(int type) {
        if (1 == type) {
            return new AmdCpu();
        } else if (2 == type) {
            return new MSIMainboard();
        }
        return null;
    }
}
public class Schema2 implements AbstractFactory{

    @Override
    public Object createProduct(int type) {
        if (1 == type) {
            return new IntelCpu();
        } else if (2 == type) {
            return new GAMainboard();
        }
        return null;
    }
}
public class ComputerEngineer {

    void makeComputer(AbstractFactory schema) {
        CpuApi cpu = (CpuApi) schema.createProduct(1);
        MainboardApi mainboard = (MainboardApi) schema.createProduct(2);

        mainboard.installCpu();
        cpu.calulate();
    }
}
public class TestClient {
    public static void main(String[] args) {
        ComputerEngineer engineer = new ComputerEngineer();
        engineer.makeComputer(new Schema1());
    }
}

你可能感兴趣的:(GoF 创建型 抽象工厂模式)