• 设计模式之-抽象工厂(Abstract Factory Pattern)


    抽象工厂是一种创建模式,类似于普通工厂模式,它更像是工厂厂房。

    如果你熟悉Java的工厂设计模式你会发现,我们根据所提供的输入,工厂类返回不同的子使用if-else或switch语句来实现这个单一的工厂类

    在抽象工厂类中中我们摆脱if..else代码块去判断,每个子类都会有一个工厂类,然后一个抽象工厂类,将返回根据输入工厂类子类起初,这似乎令人困惑,但一旦你看到的实现很容易掌握和了解工厂和抽象工厂模式之间的细微差别。


    超类和子类(Super Class and Sub-Classes):

    package com.journaldev.design.model;
      
    public abstract class Computer {
          
        public abstract String getRAM();
        public abstract String getHDD();
        public abstract String getCPU();
          
        @Override
        public String toString(){
            return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+", CPU="+this.getCPU();
        }
    }

    package com.journaldev.design.model;
      
      
    public class PC extends Computer {
      
        private String ram;
        private String hdd;
        private String cpu;
          
        public PC(String ram, String hdd, String cpu){
            this.ram=ram;
            this.hdd=hdd;
            this.cpu=cpu;
        }
        @Override
        public String getRAM() {
            return this.ram;
        }
      
        @Override
        public String getHDD() {
            return this.hdd;
        }
      
        @Override
        public String getCPU() {
            return this.cpu;
        }
      
    }

    package com.journaldev.design.model;
      
      
    public class Server extends Computer {
      
        private String ram;
        private String hdd;
        private String cpu;
          
        public Server(String ram, String hdd, String cpu){
            this.ram=ram;
            this.hdd=hdd;
            this.cpu=cpu;
        }
        @Override
        public String getRAM() {
            return this.ram;
        }
      
        @Override
        public String getHDD() {
            return this.hdd;
        }
      
        @Override
        public String getCPU() {
            return this.cpu;
        }
      
    }

    对应每个子类的工厂类

    首先我们创建抽象工厂接口或抽象类

    ComputerAbstractFactory.java

    package com.journaldev.design.abstractfactory;
     
    import com.journaldev.design.model.Computer;
     
    public interface ComputerAbstractFactory {
     
        public Computer createComputer();
     
    }

    注意createComputer返回的是超类(super class)Computer。现在我们的工厂类实现这个接口,并返回各自的子类

    PCFactory.java

    package com.journaldev.design.abstractfactory;
     
    import com.journaldev.design.model.Computer;
    import com.journaldev.design.model.PC;
     
    public class PCFactory implements ComputerAbstractFactory {
     
        private String ram;
        private String hdd;
        private String cpu;
         
        public PCFactory(String ram, String hdd, String cpu){
            this.ram=ram;
            this.hdd=hdd;
            this.cpu=cpu;
        }
        @Override
        public Computer createComputer() {
            return new PC(ram,hdd,cpu);
        }
     
    }

    ServerFactory.java

    package com.journaldev.design.abstractfactory;
     
    import com.journaldev.design.model.Computer;
    import com.journaldev.design.model.Server;
     
    public class ServerFactory implements ComputerAbstractFactory {
     
        private String ram;
        private String hdd;
        private String cpu;
         
        public ServerFactory(String ram, String hdd, String cpu){
            this.ram=ram;
            this.hdd=hdd;
            this.cpu=cpu;
        }
         
        @Override
        public Computer createComputer() {
            return new Server(ram,hdd,cpu);
        }
     
    }

    现在我们创建消费这类(consumer class)提供客户的访问入口用来创建子类

    package com.journaldev.design.abstractfactory;
     
    import com.journaldev.design.model.Computer;
     
    public class ComputerFactory {
     
        public static Computer getComputer(ComputerAbstractFactory factory){
            return factory.createComputer();
        }
    }

    注意这个类getComputer方法接受ComputerAbstractFactory参数并返回Computer对象。这一点实现必须明确。

    让我们写一个测试类,如何使用抽象工厂方法获取子类实例

    package com.journaldev.design.test;
     
    import com.journaldev.design.abstractfactory.PCFactory;
    import com.journaldev.design.abstractfactory.ServerFactory;
    import com.journaldev.design.factory.ComputerFactory;
    import com.journaldev.design.model.Computer;
     
    public class TestDesignPatterns {
     
        public static void main(String[] args) {
            testAbstractFactory();
        }
     
        private static void testAbstractFactory() {
            Computer pc = com.journaldev.design.abstractfactory.ComputerFactory.getComputer(new PCFactory("2 GB","500 GB","2.4 GHz"));
            Computer server = com.journaldev.design.abstractfactory.ComputerFactory.getComputer(new ServerFactory("16 GB","1 TB","2.9 GHz"));
            System.out.println("AbstractFactory PC Config::"+pc);
            System.out.println("AbstractFactory Server Config::"+server);
        }
    }

    上述输出结果:

    AbstractFactory PC Config::RAM= 2 GB, HDD=500 GB, CPU=2.4 GHz
    AbstractFactory Server Config::RAM= 16 GB, HDD=1 TB, CPU=2.9 GHz

    抽象工厂类实现示意图:



    抽象工厂设计好处:

    1,抽象工厂模式提供的方法来规范接口而不是具体实现

    2,抽象工厂是工厂的工厂和容易实现产品的扩展,例如我们可以添加另一个子类台灯和台灯工厂

    3,抽象工厂模式是稳健的,避免普通工厂模式的条件判断的逻辑


    抽象工厂在jdk中的例子:

    • javax.xml.parsers.DocumentBuilderFactory#newInstance()
    • javax.xml.transform.TransformerFactory#newInstance()
    • javax.xml.xpath.XPathFactory#newInstance()

    原文地址:http://www.journaldev.com/1418/abstract-factory-design-pattern-in-java

    http://www.journaldev.com/1392/factory-design-pattern-in-java






  • 相关阅读:
    vue2.0使用基础
    docker 镜像中心搭建
    dubbo使用multicast注册方式消费者无法发现服务的一种情况(我遇到的情况)
    Bootstrap 避免模态框在用户点击背景空白处时,会自动关闭。
    HNOI2019滚粗记
    THUSC 2018 酱油记
    # HNOI2018滚粗记
    第二类斯特林数总结
    PKUWC 2018游记
    NOIP2017滚粗记
  • 原文地址:https://www.cnblogs.com/happyxiaoyu02/p/6818936.html
Copyright © 2020-2023  润新知