抽象工廠模式是一個(gè)超級(jí)工廠,用來(lái)創(chuàng)建其他工廠。 這個(gè)工廠也被稱為工廠的工廠。 這種類型的設(shè)計(jì)模式屬于創(chuàng)建模式,因?yàn)榇四J教峁┝藙?chuàng)建對(duì)象的最佳方法之一。
在抽象工廠模式中,接口負(fù)責(zé)創(chuàng)建相關(guān)對(duì)象的工廠,而不明確指定它們的類。 每個(gè)生成的工廠可以按照工廠模式提供對(duì)象。
我們將創(chuàng)建一個(gè)Shape
和Color
接口并實(shí)現(xiàn)這些接口的具體類。在下一步中,將創(chuàng)建一個(gè)抽象工廠類AbstractFactory
。在每個(gè)工廠類ShapeFactory
和ColorFactory
定義都是擴(kuò)展自AbstractFactory
。創(chuàng)建工廠創(chuàng)建者/生成器類 - FactoryProducer
。
AbstractFactoryPatternDemo
這是一個(gè)演示類,使用FactoryProducer
來(lái)獲取AbstractFactory
對(duì)象。 它會(huì)將信息(CIRCLE
/ RECTANGLE
/ SQUARE
)傳遞給AbstractFactory
以獲取所需的對(duì)象類型。 它還將信息(用于Color
的 RED
/ GREEN
/ BLUE
)傳遞給AbstractFactory
以獲取所需的對(duì)象類型。
創(chuàng)建Shape
的接口。
Shape.java
public interface Shape {
void draw();
}
創(chuàng)建實(shí)現(xiàn)相同接口的具體類。
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.");
}
}
創(chuàng)建一個(gè)Colors
接口,如下所示
Color.java
public interface Color {
void fill();
}
創(chuàng)建實(shí)現(xiàn)相同接口的具體類。
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.");
}
}
創(chuàng)建實(shí)現(xiàn)相同接口的具體類。
AbstractFactory.java
public abstract class AbstractFactory {
abstract Color getColor(String color);
abstract Shape getShape(String shape) ;
}
創(chuàng)建實(shí)現(xiàn)相同接口的具體類。
創(chuàng)建工廠類,根據(jù)給定信息擴(kuò)展AbstractFactory
以生成具體類的對(duì)象。
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;
}
}
創(chuàng)建工廠生成器/生產(chǎn)器類,通過(guò)傳遞如Shape
或Color
等信息來(lái)獲取工廠
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;
}
}
使用FactoryProducer
來(lái)獲取AbstractFactory
,以便通過(guò)傳遞類型等信息來(lái)獲取具體類的工廠。
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();
}
}
驗(yàn)證輸出,結(jié)果如下 -
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside Red::fill() method.
Inside Green::fill() method.
Inside Blue::fill() method.