设计模式——工厂模式

1、简介

工厂模式主要有三类:简单工厂模式、工厂模式、抽象工厂模式。

简单工厂模式:根据传入的参数,创建新的对应对象。当增加新的对象时,需要修改工厂类内部的结构,违反“开放-封闭原则”

工厂模式:具有工厂接口与产品接口,不同工厂生产不同的产品,遵守了”开放-封闭“原则。

抽象工厂模式:可以通过工厂类生产一批不同类别的产品。

使用较多的是工厂模式。

三种设计模式的详细分析如下。

2、详解

(1)简单工厂模式

简单工厂模式就是由一个工厂类根据传入的参数决定创建哪一种的产品类。

有4个角色:

a.工厂类角色:是具体产品类角色直接调用者;

b.抽象产品角色:接口或抽象类,负责具体产品角色的定义,及与客户端的交互。

c.具体产品角色:被工厂类创建的对象,也是客户端实际操作对象。

d.客户端:调用工厂类产生实例,并调用实例的方法进行相应工作。

 1 public interface people{
 2       public void say();
 3 }
 4 
 5 public class chinese implements people{
 6       public void say(){
 7            System.out.println("说中国话");
 8      }
 9 }
10 
11 public class american implements people{
12       public void say(){
13            System.out.println("speak english");
14      }
15 } 
16 
17 public class peopleFactory{
18       public static people create(int type){
19            if(type==1){
20                   return new chinese();
21            }else if(type==2){
22                  return new american();
23            }
24      }
25  }
26 
27 public class test{
28       public static void main(String []args){
29            people p=peopleFactory.create(1);
30            p.say();
31            p=peopleFactory.create(2);
32             p.say();
33      }
34  }

(2)工厂模式

工厂方法模式对简单工厂进行了抽象,拥有一个抽象的工厂类,定制接口规范。具体的生产工作交给子类。还有一个抽象的产品类,定义一些规范。具体属性由子类定义。抽象工厂对应抽象产品。具体的某个工厂类生产对应的具体产品。实体工厂实现抽象工厂,实体产品实现抽象产品;抽象工厂生产抽象产品,实体工厂生产实体产品;实体工厂A生产实体产品A,实体工厂B生产实体产品B。

 1 public interface Icar{
 2            public void docar();
 3 } 
 4 
 5 public class bwm implements Icar{
 6           public void docar(){
 7                System.out.println("我是宝马,别摸我");
 8           }
 9 }
10 
11  public class buick implements Icar{
12           public void docar(){
13                System.out.println("我是别克,很酷");
14           }
15 }
16 
17 public interface Icarfactory{
18           public Icar createCar();
19 } 
20 
21 public class bmwFactory implements Icarfactory{
22           public Icar createCar(){
23                    return new bwm();
24           }
25 } 
26 
27 public class buickFactory implements Icarfactory{
28           public Icar createCar(){
29                    return new buick();
30           }
31 } 
32  
33 public class test{
34       public static void main(String []args){
35            Icarfactory factory=new bmwFactory();
36            Icar bwm= factory.createCar(); 
37            bwm.docar();
38 
39            factory=new buickFactory();
40            Icar buick= factory.createCar(); 
41            buick.docar();
42      }
43  }

(3)抽象工厂模式

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

通过子工厂的父工厂,产生子工厂,用子工厂去生产不同的产品。

具体代码如下:

  1 public interface Shape {
  2    void draw();
  3 }
  4 
  5 public class Rectangle implements Shape {
  6 
  7    @Override
  8    public void draw() {
  9       System.out.println("Inside Rectangle::draw() method.");
 10    }
 11 }
 12 
 13 public class Square implements Shape {
 14 
 15    @Override
 16    public void draw() {
 17       System.out.println("Inside Square::draw() method.");
 18    }
 19 }
 20 
 21 public class Circle implements Shape {
 22 
 23    @Override
 24    public void draw() {
 25       System.out.println("Inside Circle::draw() method.");
 26    }
 27 }
 28 
 29 public interface Color {
 30    void fill();
 31 }
 32 
 33 public class Red implements Color {
 34 
 35    @Override
 36    public void fill() {
 37       System.out.println("Inside Red::fill() method.");
 38    }
 39 }
 40 
 41 public class Green implements Color {
 42 
 43    @Override
 44    public void fill() {
 45       System.out.println("Inside Green::fill() method.");
 46    }
 47 }
 48 
 49 public class Blue implements Color {
 50 
 51    @Override
 52    public void fill() {
 53       System.out.println("Inside Blue::fill() method.");
 54    }
 55 }
 56 
 57 public abstract class AbstractFactory {
 58    abstract Color getColor(String color);
 59    abstract Shape getShape(String shape) ;
 60 }
 61 
 62 public class ShapeFactory extends AbstractFactory {
 63     
 64    @Override
 65    public Shape getShape(String shapeType){
 66       if(shapeType == null){
 67          return null;
 68       }        
 69       if(shapeType.equalsIgnoreCase("CIRCLE")){
 70          return new Circle();
 71       } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
 72          return new Rectangle();
 73       } else if(shapeType.equalsIgnoreCase("SQUARE")){
 74          return new Square();
 75       }
 76       return null;
 77    }
 78    
 79    @Override
 80    Color getColor(String color) {
 81       return null;
 82    }
 83 }
 84 
 85 public class ColorFactory extends AbstractFactory {
 86     
 87    @Override
 88    public Shape getShape(String shapeType){
 89       return null;
 90    }
 91    
 92    @Override
 93    Color getColor(String color) {
 94       if(color == null){
 95          return null;
 96       }        
 97       if(color.equalsIgnoreCase("RED")){
 98          return new Red();
 99       } else if(color.equalsIgnoreCase("GREEN")){
100          return new Green();
101       } else if(color.equalsIgnoreCase("BLUE")){
102          return new Blue();
103       }
104       return null;
105    }
106 }
107 
108 public class FactoryProducer {
109    public static AbstractFactory getFactory(String choice){
110       if(choice.equalsIgnoreCase("SHAPE")){
111          return new ShapeFactory();
112       } else if(choice.equalsIgnoreCase("COLOR")){
113          return new ColorFactory();
114       }
115       return null;
116    }
117 }
118 
119 public class AbstractFactoryPatternDemo {
120    public static void main(String[] args) {
121 
122       //获取形状工厂
123       AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
124 
125       //获取形状为 Circle 的对象
126       Shape shape1 = shapeFactory.getShape("CIRCLE");
127 
128       //调用 Circle 的 draw 方法
129       shape1.draw();
130 
131       //获取形状为 Rectangle 的对象
132       Shape shape2 = shapeFactory.getShape("RECTANGLE");
133 
134       //调用 Rectangle 的 draw 方法
135       shape2.draw();
136       
137       //获取形状为 Square 的对象
138       Shape shape3 = shapeFactory.getShape("SQUARE");
139 
140       //调用 Square 的 draw 方法
141       shape3.draw();
142 
143       //获取颜色工厂
144       AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
145 
146       //获取颜色为 Red 的对象
147       Color color1 = colorFactory.getColor("RED");
148 
149       //调用 Red 的 fill 方法
150       color1.fill();
151 
152       //获取颜色为 Green 的对象
153       Color color2 = colorFactory.getColor("Green");
154 
155       //调用 Green 的 fill 方法
156       color2.fill();
157 
158       //获取颜色为 Blue 的对象
159       Color color3 = colorFactory.getColor("BLUE");
160 
161       //调用 Blue 的 fill 方法
162       color3.fill();
163    }
164 }

声明:本文部分内容来自以下两处,在此对作者表示感谢!

http://wxg6203.iteye.com/blog/740229

http://www.runoob.com/design-pattern/abstract-factory-pattern.html

posted @ 2016-09-19 13:58  suyesean  阅读(122)  评论(0编辑  收藏  举报