抽象工厂模式

 抽象工厂模式:

  抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。通过一个工厂的生成器来创建所需要的工厂,再由对应的工厂创建对应的对象。缺点:当需要扩展时,需要在原先的代码上进行扩展,容易把原先的代码误操作改掉。

1 //为颜色创建接口
2 public interface Color {
3     public abstract void addColor();
4 }
1 public class Red implements Color {
2     @Override
3     public void addColor() {
4         System.out.println("Red::addColor() method");
5     }
6 }
1 public class Blue implements Color{
2     @Override
3     public void addColor() {
4         System.out.println("Blue::addColor() method");
5     }
6 
7 }
1 //为形状创建接口
2 public interface Shape {
3     public abstract void draw();
4 }
1 public class Square implements Shape{
2     @Override
3     public void draw() {
4         System.out.println("Square::draw() method");
5     }
6 
7 }
1 public class Rectangle implements Shape {
2     @Override
3     public void draw() {
4         System.out.println("Rectangle::draw() method");
5     }
6 
7 }
1 //为Color和Shape对象创建抽象类类获取工厂
2 public abstract class AbstractFactory {
3     public abstract Color getColor(String color);
4     public abstract Shape getShape(String sharp);
5 }
 1 //颜色的工厂
 2 public class ColorFactory extends AbstractFactory {
 3     @Override
 4     public Color getColor(String color) {
 5         if (color == null) {
 6             return null;
 7         }
 8         if (color.equalsIgnoreCase("RED")) {
 9             return new Red();
10         } else if (color.equalsIgnoreCase("BLUE")) {
11             return new Blue();
12         }
13         return null;
14     }
15 
16     @Override
17     public Shape getShape(String sharp) {
18         // TODO Auto-generated method stub
19         return null;
20     }
21 }
 1 //形状的工厂
 2 public class ShapeFactory extends AbstractFactory {
 3     @Override
 4     public Shape getShape(String shapeType) {
 5         if (shapeType == null) {
 6             return null;
 7         } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
 8             return new Rectangle();
 9         } else if (shapeType.equalsIgnoreCase("SQUARE")) {
10             return new Square();
11         }
12         return null;
13     }
14 
15     @Override
16     public Color getColor(String color) {
17         return null;
18     }
19 }
 1 //创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
 2 public class FactoryProducer {
 3    public static AbstractFactory getFactory(String choice){
 4       if(choice.equalsIgnoreCase("SHAPE")){
 5          return new ShapeFactory();
 6       } else if(choice.equalsIgnoreCase("COLOR")){
 7          return new ColorFactory();
 8       }
 9       return null;
10    }
11 }

测试类:

 1 //测试类
 2 //使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象
 3 public class AbstractFactoryPatternDemo {
 4     public static void main(String[] args) {
 5 
 6         // 获取形状工厂
 7         AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 8 
 9         // 获取形状为 Rectangle 的对象
10         Shape shape2 = (Shape) shapeFactory.getShape("RECTANGLE");
11 
12         // 调用 Rectangle 的 draw 方法
13         shape2.draw();
14 
15         // 获取形状为 Square 的对象
16         Shape shape3 = (Shape) shapeFactory.getShape("SQUARE");
17 
18         // 调用 Square 的 draw 方法
19         shape3.draw();
20 
21         // 获取颜色工厂
22         AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
23 
24         // 获取颜色为 Red 的对象
25         Color color1 = colorFactory.getColor("RED");
26 
27         // 调用 Red 的 fill 方法
28         color1.addColor();
29 
30 
31         // 获取颜色为 Blue 的对象
32         Color color3 = colorFactory.getColor("BLUE");
33 
34         // 调用 Blue 的 fill 方法
35         color3.addColor();
36     }
37 }

运行结果:

Rectangle::draw() method
Square::draw() method
Red::addColor() method
Blue::addColor() method

UML图:

  

 。

不能只满足于写完代码运行结果正确就完事,时常考虑如何让代码更加简练更加容易维护、容易扩展和复用,只有这样才可以真正得到提高 --《来自大话设计模式》
原文地址:https://www.cnblogs.com/lixianyuan-org/p/9439950.html