抽象工厂模式一般有一个超级工厂,这个工厂能够创建其它的工厂,也被称为"工厂的工厂"。同简单工厂模式样,这种设计模式也属于创造模式
实现
首先,定义接口Shape,以及其实现类。
其次,定义一个抽象工厂类AbstractFactory。工厂类ShapeFactory以及RoundedShapeFactory继承这个抽象类。FactoryProducer就是创建其它工厂的工厂类。
最后,我们的demo类AbstractFactoryPatternDemo使用超级工厂FactoryProducer来获取AbstractFactory对象(ShapeFactory或者RoundedShapeFactory对象)
下面用一张UML类图来说明这种关系
1. 定义接口
public interface Shape {
void draw();
}
2. 定义接口的实现类
RoundedRectangle.java
public class RoundedRectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedRectangle::draw() method.");
}
}
RoundedSquare.java
public class RoundedSquare implements Shape {
@Override
public void draw() {
System.out.println("Inside RoundedSquare::draw() method.");
}
}
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 {
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
Rectangle.java
public class Rectangle implements Shape {
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
3. 定义抽象工厂类
. AbstractFactory.java
public abstract class AbstractFactory {
abstract Shape getShape(String shapeType) ;
}
4. 定义工厂类继承抽象工厂类,根据传入的参数生成不同的对象。
ShapeFactory.java
public class ShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new Rectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new Square();
}
return null;
}
}
RoundedShapeFactory.java
public class RoundedShapeFactory extends AbstractFactory {
@Override
public Shape getShape(String shapeType){
if(shapeType.equalsIgnoreCase("RECTANGLE")){
return new RoundedRectangle();
}else if(shapeType.equalsIgnoreCase("SQUARE")){
return new RoundedSquare();
}
return null;
}
}
5. 定义超级工厂类,也就是工厂的工厂
可以根据入参的不同,生成不同的工厂实例。
FactoryProducer.java
ublic class FactoryProducer {
//根据参数,产生不同的工厂实例
public static AbstractFactory getFactory(boolean rounded){
if(rounded){
return new RoundedShapeFactory();
}else{
return new ShapeFactory();
}
}
}
6. 定义demo类
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
//get rounded shape factory
AbstractFactory shapeFactory = FactoryProducer.getFactory(false);
//get an object of Shape Rounded Rectangle
Shape shape1 = shapeFactory.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape1.draw();
//get an object of Shape Rounded Square
Shape shape2 = shapeFactory.getShape("SQUARE");
//call draw method of Shape Square
shape2.draw();
//get rounded shape factory
AbstractFactory shapeFactory1 = FactoryProducer.getFactory(true);
//get an object of Shape Rectangle
Shape shape3 = shapeFactory1.getShape("RECTANGLE");
//call draw method of Shape Rectangle
shape3.draw();
//get an object of Shape Square
Shape shape4 = shapeFactory1.getShape("SQUARE");
//call draw method of Shape Square
shape4.draw();
}
}
7. 运行结果
Inside Rectangle::draw() method.
Inside Square::draw() method.
Inside RoundedRectangle::draw() method.
Inside RoundedSquare::draw() method.