抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
实现
我们将创建 Shape 和 Color 接口和实现这些接口的实体类。下一步是创建抽象工厂类 AbstractFactory*。接着定义工厂类 *ShapeFactory 和 ColorFactory,这两个工厂类都是扩展了 AbstractFactory。然后创建一个工厂创造器/生成器类 FactoryProducer。
AbstractFactoryPatternDemo*,我们的演示类使用 *FactoryProducer 来获取 AbstractFactory 对象。它将向 AbstractFactory 传递形状信息 Shape(CIRCLE / RECTANGLE / SQUARE*),以便获取它所需对象的类型。同时它还向 *AbstractFactory 传递颜色信息 Color(RED / GREEN / BLUE),以便获取它所需对象的类型。
步骤1
为形状创建一个接口
1 2 3 4 5 6 7 8 9 10
| package class类.抽象工厂模式;
public interface Shape { void draw(); }
|
步骤2
创建实现接口的实体类
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Rect implements Shape{ @Override public void draw() { System.out.println("这是一个矩形!"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Square implements Shape{ @Override public void draw() { System.out.println("这是一个正方形!"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Circle implements Shape{ @Override public void draw() { System.out.println("这是一个圆形!"); } }
|
步骤3
为颜色创建一个接口
1 2 3 4 5 6 7 8 9
| package class类.抽象工厂模式;
public interface Color { void fill(); }
|
步骤4
创建实现接口的实体类
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Red implements Color{ @Override public void fill() { System.out.println("红色!"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Blue implements Color{ @Override public void fill() { System.out.println("蓝色!"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12
| package class类.抽象工厂模式;
public class Green implements Color{ @Override public void fill() { System.out.println("绿色!"); } }
|
步骤5
为Color和Shape创建抽象工厂来获取工厂
1 2 3 4 5 6 7 8 9 10
| package class类.抽象工厂模式;
public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape); }
|
步骤6
创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package class类.抽象工厂模式;
public class ShapeFactory extends AbstractFactory { @Override Color getColor(String color) { return null; }
@Override Shape getShape(String shape) { Shape shape1 = null; if (shape.equalsIgnoreCase("CIRCLE")){ shape1 = new Circle(); }else if (shape.equalsIgnoreCase("RECT")){ shape1 = new Rect(); }else if (shape.equalsIgnoreCase("SQUARE")){ shape1 = new Square(); }else { throw new RuntimeException("非法参数1!"); } return shape1; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package class类.抽象工厂模式;
public class ColorFactory extends AbstractFactory { @Override Color getColor(String color) { Color color1 = null; if (color.equalsIgnoreCase("RED")){ color1 = new Red(); }else if (color.equalsIgnoreCase("GREEN")){ color1 = new Green(); }else if (color.equalsIgnoreCase("BLUE")){ color1 = new Blue(); }else { throw new RuntimeException("非法参数!"); } return color1; }
@Override Shape getShape(String shape) { return null; } }
|
步骤7
创建一个工厂创建器,通过传递形状或颜色信息来获取工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package class类.抽象工厂模式;
public class FactoryProducer { public static AbstractFactory getFactory(String type){ AbstractFactory factory = null; if (type.equalsIgnoreCase("SHAPE")){ factory = new ShapeFactory(); }else if (type.equalsIgnoreCase("COLOR")){ factory = new ColorFactory(); }else { throw new RuntimeException("非法参数!"); }
return factory; } }
|
步骤8
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package class类.抽象工厂模式;
public class Test { @org.junit.Test public void test(){ AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
Shape circle = shapeFactory.getShape("CIRCLE");
circle.draw();
AbstractFactory colorFactory = FactoryProducer.getFactory("Color"); Color red = colorFactory.getColor("RED"); red.fill(); } }
|
