山月云星

导航

设计模式——创建性模式之抽象工厂模式

抽象工厂模式

  抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。主要解决接口选择的问题。

  优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

  缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

实现

  我们将创建 ShapeColor 接口和实现这些接口的实体类。下一步是创建抽象工厂类 AbstractFactory。接着定义工厂类 ShapeFactoryColorFactory,这两个工厂类都是扩展了 AbstractFactory。然后创建一个工厂创造器/生成器类 FactoryProducer

AbstractFactoryPatternDemo,我们的演示类使用 FactoryProducer 来获取 AbstractFactory 对象。它将向 AbstractFactory 传递形状信息 ShapeCIRCLE / RECTANGLE / SQUARE),以便获取它所需对象的类型。同时它还向 AbstractFactory 传递颜色信息 ColorRED / GREEN / BLUE),以便获取它所需对象的类型。

 

 步骤 1

  为形状创建一个接口。

1 public interface Shape {
2    void draw();
3 }

步骤 2

  创建实现接口的实体类。

1 public class Rectangle implements Shape {
2  
3    @Override
4    public void draw() {
5       System.out.println("Inside Rectangle::draw() method.");
6    }
7 }
1 public class Square implements Shape {
2  
3    @Override
4    public void draw() {
5       System.out.println("Inside Square::draw() method.");
6    }
7 }
1 public class Circle implements Shape {
2  
3    @Override
4    public void draw() {
5       System.out.println("Inside Circle::draw() method.");
6    }
7 }

步骤 3

  为颜色创建一个接口

1 public interface Color {
2    void fill();
3 }

步骤4

  创建实现接口的实体类。

1 public class Red implements Color {
2  
3    @Override
4    public void fill() {
5       System.out.println("Inside Red::fill() method.");
6    }
7 }
1 public class Green implements Color {
2  
3    @Override
4    public void fill() {
5       System.out.println("Inside Green::fill() method.");
6    }
7 }
1 public class Blue implements Color {
2  
3    @Override
4    public void fill() {
5       System.out.println("Inside Blue::fill() method.");
6    }
7 }

步骤 5

  为 Color 和 Shape 对象创建抽象类来获取工厂。

1 public abstract class AbstractFactory {
2    public abstract Color getColor(String color);
3    public abstract Shape getShape(String shape) ;
4 }

步骤 6

创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。

 1 public class ShapeFactory extends AbstractFactory {
 2     
 3    @Override
 4    public Shape getShape(String shapeType){
 5       if(shapeType == null){
 6          return null;
 7       }        
 8       if(shapeType.equalsIgnoreCase("CIRCLE")){
 9          return new Circle();
10       } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
11          return new Rectangle();
12       } else if(shapeType.equalsIgnoreCase("SQUARE")){
13          return new Square();
14       }
15       return null;
16    }
17    
18    @Override
19    public Color getColor(String color) {
20       return null;
21    }
22 }
 1 public class ColorFactory extends AbstractFactory {
 2     
 3    @Override
 4    public Shape getShape(String shapeType){
 5       return null;
 6    }
 7    
 8    @Override
 9    public Color getColor(String color) {
10       if(color == null){
11          return null;
12       }        
13       if(color.equalsIgnoreCase("RED")){
14          return new Red();
15       } else if(color.equalsIgnoreCase("GREEN")){
16          return new Green();
17       } else if(color.equalsIgnoreCase("BLUE")){
18          return new Blue();
19       }
20       return null;
21    }
22 }

步骤 7

  创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。

 

 1 public class FactoryProducer {
 2    public static AbstractFactory getFactory(String choice){
 3       if(choice.equalsIgnoreCase("SHAPE")){
 4          return new ShapeFactory();
 5       } else if(choice.equalsIgnoreCase("COLOR")){
 6          return new ColorFactory();
 7       }
 8       return null;
 9    }
10 }

步骤 8

  使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。

 1 public class AbstractFactoryPatternDemo {
 2    public static void main(String[] args) {
 3  
 4       //获取形状工厂
 5       AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 6  
 7       //获取形状为 Circle 的对象
 8       Shape shape1 = shapeFactory.getShape("CIRCLE");
 9  
10       //调用 Circle 的 draw 方法
11       shape1.draw();
12  
13       //获取形状为 Rectangle 的对象
14       Shape shape2 = shapeFactory.getShape("RECTANGLE");
15  
16       //调用 Rectangle 的 draw 方法
17       shape2.draw();
18       
19       //获取形状为 Square 的对象
20       Shape shape3 = shapeFactory.getShape("SQUARE");
21  
22       //调用 Square 的 draw 方法
23       shape3.draw();
24  
25       //获取颜色工厂
26       AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
27  
28       //获取颜色为 Red 的对象
29       Color color1 = colorFactory.getColor("RED");
30  
31       //调用 Red 的 fill 方法
32       color1.fill();
33  
34       //获取颜色为 Green 的对象
35       Color color2 = colorFactory.getColor("Green");
36  
37       //调用 Green 的 fill 方法
38       color2.fill();
39  
40       //获取颜色为 Blue 的对象
41       Color color3 = colorFactory.getColor("BLUE");
42  
43       //调用 Blue 的 fill 方法
44       color3.fill();
45    }
46 }

步骤 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.

 (以上知识及实列出自runoob.com 原文链接:https://www.runoob.com/design-pattern/abstract-factory-pattern.html)

 

 

posted on 2019-09-02 09:21  山月云星  阅读(176)  评论(0编辑  收藏  举报