Java~设计模式之工厂模式与抽象工厂模式

工厂模式

  • 工厂模式顾名思义就好比一个大的工厂里面有很多部门, 你只需要把你想要的东西交给这个工厂, 其他不用你管, 这个工厂就会给你生成出来你想要的东西, 就好比我下面代码的例子, 你说你想要手机, 你把配置给工厂说, 工厂就会判断这个HUAWEI手机还是苹果手机 最后创建手机 返回给你
  • 工厂模式一种创建对象的模式,它被广泛应用在jdk中以及Spring和Struts框架中;
  • 工厂模式基于"输入",应用在超类和多个子类之间的情况,这种模式将创建对象的责任转移到工厂类;

接口

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:16
 */
public interface Phone {
    
    

    String showCPU();
    String showRAM();
    String showSystem();
    String showBattery();

}

实例1

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:19
 */
public class IPhone implements Phone {
    
    

    private String cpu;
    private String ram;
    private String system;
    private int battery;

    public IPhone(String cpu, String ram, String system, int battery) {
    
    
        this.cpu = cpu;
        this.ram = ram;
        this.system = system;
        this.battery = battery;
    }

    @Override
    public String showCPU() {
    
    
        return "IPhone:" + this.cpu;
    }

    @Override
    public String showRAM() {
    
    
        return "IPhone:" + this.ram;
    }

    @Override
    public String showSystem() {
    
    
        return "IPhone:" + this.system;
    }

    @Override
    public String showBattery() {
    
    
        return "IPhone:" + this.battery;
    }
}

实例二

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:27
 */
public class HuaWei implements Phone {
    
    

    private String cpu;
    private String ram;
    private String system;
    private int battery;

    public HuaWei(String cpu, String ram, String system, int battery) {
    
    
        this.cpu = cpu;
        this.ram = ram;
        this.system = system;
        this.battery = battery;
    }

    @Override
    public String showCPU() {
    
    
        return "HuaWei:" + this.cpu;
    }

    @Override
    public String showRAM() {
    
    
        return "HuaWei:" + this.ram;
    }

    @Override
    public String showSystem() {
    
    
        return "HuaWei:" + this.system;
    }

    @Override
    public String showBattery() {
    
    
        return "HuaWei:" + this.battery;
    }
}

工厂

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:30
 */
public class PhoneFactory {
    
    

    public static Phone makePhone(String cpu, String ram, String system, int battery) {
    
    

        if (system != null && (system.contains("ios") || system.contains("IOS"))) {
    
    
            return new IPhone(cpu, ram, system, battery);
        } else if (system != null && (system.contains("Android") || system.contains("android"))) {
    
    
            return new HuaWei(cpu, ram, system, battery);
        }
        return null;

    }

}

演示

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:37
 */
public class TestPhoneFactory {
    
    

    public static void main(String[] args) {
    
    

        Phone phone = PhoneFactory.makePhone("A14", "4G", "ios14", 2500);
        Phone phone1 = PhoneFactory.makePhone("麒麟990", "8G", "Android7", 4500);

        if (phone != null) {
    
    
            System.out.println(phone.showCPU());
            System.out.println(phone.showRAM());
            System.out.println(phone.showSystem());
            System.out.println(phone.showBattery());
        }
        System.out.println("========================");
        if (phone1 != null) {
    
    
            System.out.println(phone1.showCPU());
            System.out.println(phone1.showRAM());
            System.out.println(phone1.showSystem());
            System.out.println(phone1.showBattery());
        }

    }

}

IPhone:A14
IPhone:4G
IPhone:ios14
IPhone:2500
========================
HuaWei:麒麟990
HuaWei:8G
HuaWei:Android7
HuaWei:4500

工厂设计模式的优点

面向接口编程,体现了面向对象的思想;
将创建对象的工作转移到了工厂类;

JDK 中的工厂设计模式实例

java.util.Calendar, ResourceBundle and NumberFormat getInstance() 使用了工厂方法模式;
valueOf() 在包装类中,如Boolean, Integer 也使用了工厂方法模式;

抽象工厂模式

  • 上面说到的抽象模式只是针对一个产品 如果我要针对多个产品 比如现在要这个工厂还要生产computer, 这样的工厂就是抽象工厂
  • 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
  • 在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

Computer接口

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 15:19
 */
public interface Computer {
    
    

    String showComputer();

}

实例1

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:19
 */
public class IPhone implements Phone, Computer {
    
    

    private String cpu;
    private String ram;
    private String system;
    private int battery;

    public IPhone(String cpu, String ram, String system, int battery) {
    
    
        this.cpu = cpu;
        this.ram = ram;
        this.system = system;
        this.battery = battery;
    }

    public IPhone(){
    
    };

    @Override
    public String showCPU() {
    
    
        return "IPhone:" + this.cpu;
    }

    @Override
    public String showRAM() {
    
    
        return "IPhone:" + this.ram;
    }

    @Override
    public String showSystem() {
    
    
        return "IPhone:" + this.system;
    }

    @Override
    public String showBattery() {
    
    
        return "IPhone:" + this.battery;
    }

    @Override
    public String showComputer() {
    
    
        return "Apple: Computer";
    }
}

实例2

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:27
 */
public class HuaWei implements Phone, Computer {
    
    

    private String cpu;
    private String ram;
    private String system;
    private int battery;

    public HuaWei(String cpu, String ram, String system, int battery) {
    
    
        this.cpu = cpu;
        this.ram = ram;
        this.system = system;
        this.battery = battery;
    }

    public HuaWei(){
    
    };

    @Override
    public String showCPU() {
    
    
        return "HuaWei:" + this.cpu;
    }

    @Override
    public String showRAM() {
    
    
        return "HuaWei:" + this.ram;
    }

    @Override
    public String showSystem() {
    
    
        return "HuaWei:" + this.system;
    }

    @Override
    public String showBattery() {
    
    
        return "HuaWei:" + this.battery;
    }

    @Override
    public String showComputer() {
    
    
        return "HuaWei: Computer";
    }
}

抽象工厂

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 15:19
 */
public abstract class AbstractFactory {
    
    

    public abstract Phone makePhone(String cpu, String ram, String system, int battery);
    public abstract Computer makeComputer(String type);
}

Computer工厂

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 15:24
 */
public class ComputerFactory extends AbstractFactory  {
    
    

    @Override
    public Phone makePhone(String cpu, String ram, String system, int battery) {
    
    
        return null;
    }

    @Override
    public Computer makeComputer(String type) {
    
    
         if (type.equalsIgnoreCase("apple")) {
    
    
             return new IPhone();
         } else if (type.equalsIgnoreCase("HuaWei")) {
    
    
             return new HuaWei();
         }
         return null;
    }
}

工厂生成器

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 15:39
 */
public class FactoryProducer {
    
    

    public static AbstractFactory getFactory(String type) {
    
    
        if (type.equalsIgnoreCase("computer")) {
    
    
            return new ComputerFactory();
        } else if (type.equalsIgnoreCase("phone")) {
    
    
            return new PhoneFactory();
        }
        return null;
    }

}

演示

/**
 * Created with IntelliJ IDEA.
 * Description: If you don't work hard, you will a loser.
 * User: Listen-Y.
 * Date: 2020-08-28
 * Time: 14:37
 */
public class TestPhoneFactory {
    
    

    public static void main(String[] args) {
    
    

        AbstractFactory phoneFactory = FactoryProducer.getFactory("phone");

        assert phoneFactory != null;
        Phone phone = phoneFactory.makePhone("A14", "4G", "ios14", 2500);
        Phone phone1 = phoneFactory.makePhone("麒麟990", "8G", "Android7", 4500);
        if (phone != null) {
    
    
            System.out.println(phone.showCPU());
            System.out.println(phone.showRAM());
            System.out.println(phone.showSystem());
            System.out.println(phone.showBattery());
        }
        System.out.println("========================");
        if (phone1 != null) {
    
    
            System.out.println(phone1.showCPU());
            System.out.println(phone1.showRAM());
            System.out.println(phone1.showSystem());
            System.out.println(phone1.showBattery());
        }

        System.out.println("========================");
        AbstractFactory computerFactory = FactoryProducer.getFactory("computer");
        assert computerFactory != null;
        Computer computer = computerFactory.makeComputer("HuaWei");
        Computer computer1 = computerFactory.makeComputer("apple");
        System.out.println(computer.showComputer());
        System.out.println(computer1.showComputer());

    }

}

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/108279699