码迷,mamicode.com
首页 > 编程语言 > 详细

Java设计模式之——抽象工厂

时间:2015-12-30 10:36:08      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

 

技术分享

 

步骤 1

为形状创建一个接口。

1 packagecom.demo.mode.mode01.abstractFactory.Shape.java
2 
3 public interface Shape {
4 
5   void draw();
6 
7 }

 

步骤 2

创建实现接口的实体类。

 1 package com.demo.mode.mode01.abstractFactory.Rectangle.java
 2 
 3 public class Rectangle implements Shape {
 4 
 5   public void draw() {
 6 
 7       System.out.println("execute Rectangle::draw()method.");
 8 
 9   }
10 
11 }
12 
13 package com.demo.mode.mode01.abstractFactory.Square.java
14 
15  public class Square implements Shape {
16 
17   public void draw() {
18 
19      System.out.println("execute Circle::draw()method.");
20 
21   }
22 
23 }
24 
25 package com.demo.mode.mode01.abstractFactory.Circle.java
26 
27 public class Circle implements Shape {
28 
29   public void draw() {
30 
31      System.out.println("execute Circle::draw()method.");
32 
33   }
34 
35 }

 

步骤 3

为颜色创建一个接口。

1 package com.demo.mode.mode01.abstractFactory.Color.java
2 
3 public interface Color {
4 
5   // 填充颜色
6 
7   void fill();
8 
9 }

 

步骤4

创建实现接口的实体类。

 1 package com.demo.mode.mode01.abstractFactory.Red.java
 2 
 3 public class Red implements Color {
 4 
 5   public void fill() {
 6 
 7       System.out.println("execute Red::fill()method.");
 8 
 9   }
10 
11 }
12 
13 package com.demo.mode.mode01.abstractFactory.Green.java
14 
15 public class Green implements Color {
16 
17   public void fill() {
18 
19       System.out.println("execute Green::fill()method.");
20 
21   }
22 
23 }
24 
25 package com.demo.mode.mode01.abstractFactory.Blue.java
26 
27 public class Blue implements Color {
28 
29   public void fill() {
30 
31       System.out.println("execute Blue::fill()method.");
32 
33   }
34 
35 }

 

步骤 5

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

1 package com.demo.mode.mode01.abstractFactory.AbstractFactory.java public abstract class AbstractFactory {
2 
3   abstract Color getColor( String color);
4 
5   abstract Shape getShape( String shape);
6 
7 }

 

步骤 6

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

  1 package com.demo.mode.mode01.abstractFactory.ShapeFactory.java
  2 
  3 public class ShapeFactory  extends AbstractFactory{
  4 
  5   /**
  6 
  7    * 使用 getShape 方法获取形状类型的对象
  8 
  9    * @author xg.qiu<br/>
 10 
 11    * @since JDK 1.7
 12 
 13    * @time Jul 28, 2015
 14 
 15    * @param shapeType 形状类型
 16 
 17    * @return shape对象
 18 
 19    */
 20 
 21   public Shape getShape(String shapeType) {
 22 
 23      Shape shape = null;
 24 
 25      if ("Circle".equals(shapeType)) {
 26 
 27        shape = new Circle();
 28 
 29 } else if ("Rectangle".equals(shapeType)) {
 30 
 31        shape = new Rectangle();
 32 
 33      } else if ("Square".equals(shapeType)) {
 34 
 35        shape = new Square();
 36 
 37      }
 38 
 39      return shape;
 40 
 41   }
 42 
 43  
 44 
 45   public Color getColor(String color) {
 46 
 47      return null;
 48 
 49   }
 50 
 51 }
 52 
 53 package com.demo.mode.mode01.abstractFactory.ColorFactory.java public class ColorFactory extends AbstractFactory {
 54 
 55  
 56 
 57   /**
 58 
 59    * 使用 getColor 获得颜色
 60 
 61    * @author xg.qiu<br/>
 62 
 63    * @since JDK 1.7
 64 
 65    * @time Jul 28, 2015
 66 
 67    * @param colorStr 颜色字符串
 68 
 69    * @return color对象
 70 
 71    */
 72 
 73   public Color getColor(String colorStr) {
 74 
 75      Color color = null;
 76 
 77      if ("Red".equalsIgnoreCase(colorStr)) {
 78 
 79         color = new Red();
 80 
 81 } else if ("Green".equalsIgnoreCase(colorStr)) {
 82 
 83         color = new Green();
 84 
 85 } else if ("Blue".equalsIgnoreCase(colorStr)) {
 86 
 87         color = new Blue();
 88 
 89       }
 90 
 91      return color;
 92 
 93   }
 94 
 95  
 96 
 97   public Shape getShape(String shape) {
 98 
 99      return null;
100 
101   }
102 
103  
104 
105 }

 

步骤 7

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

 1 package com.demo.mode.mode01.abstractFactory.FactoryProducer.java
 2 
 3 public class FactoryProducer {
 4 
 5   public static AbstractFactory getFactory(String choice){
 6 
 7      AbstractFactory factory = null;
 8 
 9       if("Shape".equalsIgnoreCase(choice)){
10 
11         factory = new ShapeFactory();
12 
13       }else if("Color".equalsIgnoreCase(choice)){
14 
15         factory = new ColorFactory();
16 
17       }
18 
19       return factory;
20 
21    }
22 
23 }

 

步骤 8

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

 1 package com.demo.mode.mode01.abstractFactory.AbstractFactoryPatternDemo.java
 2 
 3 public class AbstractFactoryPatternDemo {
 4 
 5   public static void main(String[] args) {
 6 
 7      //1.获取形状
 8 
 9      AbstractFactory shapeFactory = FactoryProducer.getFactory("Shape");
10 
11      //2.获取颜色
12 
13      AbstractFactory colorFactory = FactoryProducer.getFactory("Color");
14 
15      //3.传递形状
16 
17      Shape shape =shapeFactory.getShape("Circle");
18 
19      //4.传递颜色
20 
21      Color color =colorFactory.getColor("Red");
22 
23      //5.绘制形状
24 
25      shape.draw();
26 
27      //6.填充颜色
28 
29      color.fill();   
30 
31   }
32 
33 }

 

步骤 9

验证输出。

execute Circle::draw() method.

execute Red::fill() method.

 

Java设计模式之——抽象工厂

标签:

原文地址:http://www.cnblogs.com/XQiu/p/5087893.html

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