抽象工廠模式是一個超級工廠,用來創建其他工廠。 這個工廠也被稱為工廠的工廠。 這種類型的設計模式屬於創建模式,因為此模式提供了創建對象的最佳方法之一。
在抽象工廠模式中,接口負責創建相關對象的工廠,而不明確指定它們的類。 每個生成的工廠可以按照工廠模式提供對象。
實現實例
我們將創建一個Shape和Color接口並實現這些接口的具體類。在下一步中,將創建一個抽象工廠類AbstractFactory。在每個工廠類ShapeFactory和ColorFactory定義都是擴展自AbstractFactory。創建工廠創建者/生成器類 - FactoryProducer。
AbstractFactoryPatternDemo這是一個演示類,使用FactoryProducer來獲取AbstractFactory對象。 它會將信息(CIRCLE / RECTANGLE / SQUARE)傳遞給AbstractFactory以獲取所需的對象類型。 它還將信息(用於Color的 RED / GREEN / BLUE)傳遞給AbstractFactory以獲取所需的對象類型。
第1步
創建Shape的接口。
Shape.java
public interface Shape { void draw();}
第2步
創建實現相同接口的具體類。
Rectangle.java
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}
Square.java
public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}
Circle.java
public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}
第3步
創建一個Colors接口,如下所示
Color.java
public interface Color { void fill();}
第4步
創建實現相同接口的具體類。
public class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); }}
Green.java
public class Green implements Color { @Override public void fill() { System.out.println("Inside Green::fill() method."); }}
Blue.java
public class Blue implements Color { @Override public void fill() { System.out.println("Inside Blue::fill() method."); }}
第5步
創建實現相同接口的具體類。
AbstractFactory.java
public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ;}
第6步
創建實現相同接口的具體類。
創建工廠類,根據給定信息擴展AbstractFactory以生成具體類的對象。
ShapeFactory.java
public 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 Color getColor(String color) { return null; }}
ColorFactory.java
public class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @Override Color getColor(String color) { if(color == null){ return null; } if(color.equalsIgnoreCase("RED")){ return new Red(); }else if(color.equalsIgnoreCase("GREEN")){ return new Green(); }else if(color.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; }}
第7步
創建工廠生成器/生產器類,通過傳遞如Shape或Color等信息來獲取工廠
FactoryProducer.java
public class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase("SHAPE")){ return new ShapeFactory(); }else if(choice.equalsIgnoreCase("COLOR")){ return new ColorFactory(); } return null; }}
第8步
使用FactoryProducer來獲取AbstractFactory,以便通過傳遞類型等信息來獲取具體類的工廠。
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo { 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 color factory AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); //get an object of Color Red Color color1 = colorFactory.getColor("RED"); //call fill method of Red color1.fill(); //get an object of Color Green Color color2 = colorFactory.getColor("Green"); //call fill method of Green color2.fill(); //get an object of Color Blue Color color3 = colorFactory.getColor("BLUE"); //call fill method of Color Blue color3.fill(); }}
第9步
驗證輸出,結果如下 -
Inside Circle::draw() method.Inside Rectangle::draw() method.Inside Square::draw() method.Inside Red::fill() method.Inside Green::fill() method.Inside Blue::fill() method.