抽象工厂是一种创建模式,类似于普通工厂模式,即它更像是工厂厂房。
如果你熟悉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