抽象工廠模式是另一個創(chuàng)建模式。
抽象工廠模式,也稱為工廠的工廠,有一個工廠創(chuàng)建其他工廠。
當(dāng)使用抽象工廠模式時,我們首先使用超級工廠創(chuàng)建工廠,然后使用創(chuàng)建的工廠創(chuàng)建對象。
下面的代碼展示了如何使用抽象工廠模式。
我們將要創(chuàng)建形狀和打印機(jī)。對于形狀,我們會有圓形,矩形和正方形。對于打印機(jī),我們將有紙張打印機(jī),網(wǎng)絡(luò)打印機(jī)和屏幕打印機(jī)。
對于shape,我們將創(chuàng)建Shape界面,如下所示:
interface Shape { void draw(); }
然后我們創(chuàng)建實(shí)現(xiàn)Shape接口的具體類。
class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } } class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); } }
我們?yōu)榇蛴C(jī)創(chuàng)建一個界面。
interface Printer{ void print(); }
然后我們創(chuàng)建實(shí)現(xiàn)Printer接口的具體類。
class PaperPrinter implements Printer{ @Override public void print() { System.out.println("paper"); } } class WebPrinter implements Printer{ @Override public void print() { System.out.println("web"); } } class ScreenPrinter implements Printer{ @Override public void print() { System.out.println("screen"); } }
最后,我們創(chuàng)建一個抽象類來獲取打印機(jī)和形狀對象的工廠。
abstract class AbstractFactory { abstract Printer getPrinter(String type); abstract Shape getShape(String shape) ; }
最后,我們創(chuàng)建Factory類,根據(jù)給定的信息擴(kuò)展AbstractFactory以生成具體類的對象。
class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override Printer getPrinter(String type) { return null; } } class PrinterFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @Override Printer getPrinter(String type) { if(type == null){ return null; } if(type.equalsIgnoreCase("paper")){ return new PaperPrinter(); } else if(type.equalsIgnoreCase("web")){ return new WebPrinter(); } else if(type.equalsIgnoreCase("Screen")){ return new ScreenPrinter(); } return null; } }
創(chuàng)建一個Factory生成器/生產(chǎn)器類,通過傳遞Shape或Printer等信息來獲取工廠。
class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase("SHAPE")){ return new ShapeFactory(); } else if(choice.equalsIgnoreCase("Printer")){ return new PrinterFactory(); } return null; } }
下面的代碼展示了如何使用抽象工廠模式。
public class Main { public static void main(String[] args) { //get shape factory AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); //get an object of Shape Circle Shape shape1 = shapeFactory.getShape("CIRCLE"); //call draw method of Shape Circle shape1.draw(); //get an object of Shape Rectangle Shape shape2 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape2.draw(); //get an object of Shape Square Shape shape3 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape3.draw(); //get printer factory AbstractFactory printerFactory = FactoryProducer.getFactory("printer"); Printer printer1 = printerFactory.getPrinter("Paper"); printer1.print(); Printer printer2 = printerFactory.getPrinter("Web"); printer2.print(); Printer printer3 = printerFactory.getPrinter("Screen"); printer3.print(); } }
上面的代碼生成以下結(jié)果。
更多建議: