三大工厂设计模式应用总结

一.题目分析
1.题目一:简单工厂模式的应用
(1)题目要求:模拟女娲造人;传入参数M,返回一个Man对象,传入参数W,返回一个Woman对象;在此实现基础上,增加一个机器人类,传入参数R,返回一个Robot对象;实现该题目要求,并且观察女娲的变化。
(2)题目应用知识点:运用简单工厂模式编程实现
2.题目二:工厂方法模式的应用
(1)题目要求:模拟不同工厂生产空调;海尔工厂生产海尔空调,美的工厂生产美的空调。
(2)题目应用知识点:运用工厂方法模式编程实现;绘制类图
3.题目三:抽象工厂模式的应用
(1)题目要求:模拟电脑硬件生产工厂生产配件;该题中生产RAM、CPU硬件设备,品牌有Mac、Pc;如下是“产品等级结构-产品族”的示意图。
(2)题目应用知识点:运用抽象工厂模式编程实现;绘制类图

二.类图设计
1.模拟女娲造人的UML类图(简单工厂模式)
在这里插入图片描述
2.模拟生产空调的UML类图(工厂方法模式)
在这里插入图片描述
3.模拟电脑配件生产工厂生产硬件配件类图(抽象方法模式)
在这里插入图片描述
三.程序实现
1.简单工厂模式应用
1.1接口

public interface IPerson {
    void printPerson();
}

1.2接口实现类
1.2.1Woman类

class Woman implements IPerson{
    @Override
    public void printPerson() {
        System.out.println("女娲造出一个女生!");
    }
}

1.2.2Man类

class Man implements IPerson{
    @Override
    public void printPerson() {
        System.out.println("女娲造出一个男生!");
    }
}

1.2.3Robot类

class Robot implements IPerson{
    @Override
    public void printPerson() {
        System.out.println("女娲娘娘造出来一个机器人!!!");
    }
}

1.3工厂类

class PersonFactory {
    public static IPerson getInstance(String type){
        IPerson person = null;
        if(type.equals("W")){
            person = new Woman();
        }else if(type.equals("M")){
            person = new Man();
        }else if(type.equals("R")){
            person = new Robot();
        }
        return person;
    }
}

1.4测试类

public class Test {

    public void createPerson(IPerson person){
        person.printPerson();
    }
    public static void main(String[] args) {
        String persontype;
        System.out.println("选择输入W或M或R:");
        Scanner sc = new Scanner(System.in);
        persontype = sc.nextLine();
        IPerson person = PersonFactory.getInstance(persontype);
        new Test().createPerson(person);
    }
}

2.工厂方法模式应用
2.1接口
2.1.1空调接口

public interface AirCondition {
    void printAirCondition();
}

2.1.2空调工厂接口

public interface AirConditionFactory {
    AirCondition createAirCondition();
}

2.2接口实现类
2.2.1HaierAirCondition类

class HaierAirCondition implements AirCondition{
    @Override
    public void printAirCondition() {
        System.out.println("生产一个海尔空调...");
    }
}

2.2.2MideaAirCondition类

class MideaAirCondition implements AirCondition{
    @Override
    public void printAirCondition() {
        System.out.println("生产一个美的空调...");
    }
}

2.2.3HaierFactory类

class HaierFactory implements AirConditionFactory{
    @Override
    public AirCondition createAirCondition() {
        return new HaierAirCondition();
    }
}

2.2.4MideaFactory类

class MideaFactory implements AirConditionFactory{
    public AirCondition createAirCondition() {
        return new MideaAirCondition();
    }
}

2.3测试类

public class Test {
    public void createAirCondition(AirCondition type){
        type.printAirCondition();
    }
    public static void main(String[] args) {
        AirConditionFactory airConditionFactory = new HaierFactory();
        new Test().createAirCondition(airConditionFactory.createAirCondition());
    }
}

3.抽象工厂模式应用
3.1接口
3.1.1CPU接口

public interface ICPU {
    void printCPU();
}

3.1.2RAM接口

public interface IRAM {
    void printRAM();
}

3.1.3生产工厂接口

public interface ProductionFactory {
    ICPU createCPU();
    IRAM createRAM();
}

3.2接口实现类
3.2.1MacCPU类

public class MacCPU implements ICPU {
    @Override
    public void printCPU() {
        System.out.println("This is a MacCPU!");
    }
}

3.2.2PcCPU类

public class PcCPU implements ICPU{
    @Override
    public void printCPU() {
        System.out.println("This is a PcCPU!");
    }
}

3.2.3MacRAM类

public class MacRAM implements IRAM{
    @Override
    public void printRAM() {
        System.out.println("This is a MacRAM!");
    }
}

3.2.4PcRAM类

public class PcRAM implements IRAM {
    @Override
    public void printRAM() {
        System.out.println("This is a PcRAM!");
    }
}

3.2.5MacProductionFactory类

public class MacProductionFactory implements ProductionFactory {
    @Override
    public ICPU createCPU() {
        return new MacCPU();
    }
    @Override
    public IRAM createRAM() {
        return new MacRAM();
    }
}

3.2.6PcProductionFactory类

public class PcProductionFactory implements ProductionFactory {
    @Override
    public ICPU createCPU() {
        return new PcCPU();
    }
    @Override
    public IRAM createRAM() {
        return new PcRAM();
    }
}

3.3测试类

public class Test {
    public void buyCPU(ICPU cpu){
        cpu.printCPU();
    }
    public void buyRAM(IRAM ram){
        ram.printRAM();
    }
    public static void main(String[] args) {
        Test test = new Test();
        ProductionFactory productionFactory = new MacProductionFactory();
        ICPU cpu = productionFactory.createCPU();
        IRAM ram = productionFactory.createRAM();
        test.buyCPU(cpu);
        test.buyRAM(ram);
    }
}

四.调试、测试及运行结果
1.题目一:
1.1当传入参数是W:
在这里插入图片描述
1.2当传入参数是M:
在这里插入图片描述
1.3当传入参数是R:
在这里插入图片描述
2.题目二:
2.1传入的是Haier空调对象:
在这里插入图片描述
2.2传入的是Midea空调对象:
在这里插入图片描述
3.题目三:
3.1传入的是Mac对象:
在这里插入图片描述
3.2 传入的是Pc对象:
在这里插入图片描述

五.总结
1.简单工厂模式:
(1)添加具体产品时,需要修改工厂类,违反了OCP原则;
(2)若需要添加多个具体产品时,会多次修改工厂类代码,导致工厂类体代码量不断增长,不利于日后代码维护;
(3)代码思想易于理解,代码编写简单;
2.工厂方法模式:
(1)区别于简单工厂模式,将产品类的对象实例化操作不再交给工厂类实现,而是将其交给具体的产品工厂类实现.
(2)产品拓展很方便,只需要增加对应的产品类、产品工厂类即可,不违反OCP原则;
3.抽象工厂模式:
(1)是工厂方法模式的一次拓展,它把原先的工厂方法模式中只能有一个抽象产品不能添加产品族 的缺点克服了,抽象工厂模式不仅仅遵循了OCP原则,而且可以添加更多产品(抽象产品),具体工厂也不仅仅 可以生成单一产品,而是生成一组产品,抽象工厂也是声明一组产品,对应扩展更加灵活;
(2)拓展族系产品很麻烦,需要修改工厂类代码;

猜你喜欢

转载自blog.csdn.net/weixin_44369212/article/details/89854221