码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2018-08-07 22:52:25      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:[]   创建   .com   eth   测试   gets   cas   extends   技术   

 

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图:

  技术分享图片

 

抽象工厂模式

标签:[]   创建   .com   eth   测试   gets   cas   extends   技术   

原文地址:https://www.cnblogs.com/lixianyuan-org/p/9439950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!