JAVA設計模式(2):抽象工廠模式
抽象工廠模式是一個超級工廠,用來建立其他工廠。 這個工廠也被稱為工廠的工廠。 這種型別的設計模式屬於建立模式,因為此模式提供了建立物件的最佳方法之一。在抽象工廠模式中,介面負責建立相關物件的工廠,而不明確指定它們的類。 每個生成的工廠可以按照工廠模式提供物件。
實現例項
我們將建立一個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步建立實現相同介面的具體類。
Red.java
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.