设计模式-工厂模式[Factory]

设计模式-工厂模式[Factory]_第1张图片

 先看下一个简单的实现:

 1 package org.masque.designpatterns.factorymethod.one;
 2 /**
 3  * 
 4  * Description: Sample子类的标示
 5  * BeanEm.java Create on 2014年7月11日 下午2:37:58 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public enum BeanEm {
11     
12     ONE,
13     
14     TWO,
15     
16     THREE;
17 }

 

package org.masque.designpatterns.factorymethod.one;
/**
 * 
 * Description:抽象父类
 * Sample.java Create on 2014年7月11日 下午3:56:01 
 * @author [email protected]
 * @version 1.0
 * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 */
public abstract class Sample {
    
    public Sample(){
        System.out.println(this);
    }
}

 

 1 package org.masque.designpatterns.factorymethod.one;
 2 /**
 3  * 
 4  * Description:子类A
 5  * SampleA.java Create on 2014年7月11日 下午3:56:32 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class SampleA extends Sample{
11 
12     public SampleA(){
13     }
14 }
 1 package org.masque.designpatterns.factorymethod.one;
 2 /**
 3  * 
 4  * Description:子类B
 5  * SampleB.java Create on 2014年7月11日 下午3:56:32 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class SampleBextends Sample{
11 
12     public SampleB(){
13     }
14 }

 

 1 package org.masque.designpatterns.factorymethod.one;
 2 
 3 /**
 4  * 
 5  * Description: BeanFactory.java Create on 2014年7月11日 下午2:38:29
 6  * 
 7  * @author [email protected]
 8  * @version 1.0 Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class BeanFactory {
11 
12     public static Sample createSample(BeanEm em) {
13         switch (em) {
14         case ONE:
15             return new SampleA();
16         case TWO:
17             return new SampleB();
18         default:
19             return null;
20         }
21     }
22 }

 

 1 package org.masque.designpatterns.factorymethod.one;
 2 /**
 3  * 
 4  * Description:测试
 5  * Test.java Create on 2014年7月11日 下午3:56:59 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Test {
11 
12     public static void main(String[] args) {
13         BeanFactory.createSample(BeanEm.ONE);
14         BeanFactory.createSample(BeanEm.TWO);
15     }
16 }

 

设计模式-工厂模式[Factory]_第2张图片

先定义一个抽象的父类

 1 package org.masque.designpatterns.factorymethod.two;
 2 
 3 /**
 4  * 
 5  * Description: Shape.java Create on 2014年7月10日 下午4:53:27
 6  * 
 7  * @author [email protected]
 8  * @version 1.0 Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public abstract class Shape {
11     // 勾画shape
12     public abstract void draw();
13 
14     // 擦去 shape
15     public abstract void erase();
16 
17     public String name;
18 
19     public Shape(String name) {
20         this.name = name;
21     }
22 }

再定义一个抽象工厂的父类

 1 package org.masque.designpatterns.factorymethod.two;
 2 
 3 public abstract class ShapeFactory {
 4     protected abstract Shape factoryMethod(String aName);
 5 
 6     // 在anOperation中定义Shape的一系列行为
 7     public void anOperation(String aName) {
 8         Shape s = factoryMethod(aName);
 9         System.out.println("The current shape is: " + s.name);
10         s.draw();
11         s.erase();
12     }
13 }

子类一:

 1 package org.masque.designpatterns.factorymethod.two;
 2 /**
 3  * 
 4  * Description:
 5  * Circle.java Create on 2014年7月10日 下午5:28:06 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Circle extends Shape {
11 
12     public void draw() {
13         System.out.println("It will draw a circle.");
14     }
15 
16     public void erase() {
17         System.out.println("It will erase a circle.");
18     }
19 
20     // 构造函数
21     public Circle(String name) {
22         super(name);
23     }
24 
25 }
 1 package org.masque.designpatterns.factorymethod.two;
 2 /**
 3  * 
 4  * Description:
 5  * CircleFactory.java Create on 2014年7月10日 下午5:28:18 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class CircleFactory extends ShapeFactory {
11     // 重载factoryMethod方法,返回Circle对象
12     protected Shape factoryMethod(String aName) {
13         return new Circle(aName + " (created by CircleFactory)");
14     }
15 }

子类二:

 1 package org.masque.designpatterns.factorymethod.two;
 2 /**
 3  * 
 4  * Description:
 5  * Square.java Create on 2014年7月10日 下午5:28:25 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Square extends Shape {
11     public void draw() {
12         System.out.println("It will draw a square.");
13     }
14 
15     public void erase() {
16         System.out.println("It will erase a square.");
17     }
18 
19     // 构造函数
20     public Square(String name) {
21         super(name);
22     }
23 }
 1 package org.masque.designpatterns.factorymethod.two;
 2 /**
 3  * 
 4  * Description:
 5  * SquareFactory.java Create on 2014年7月10日 下午5:28:31 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class SquareFactory extends ShapeFactory {
11     
12     // 重载factoryMethod方法,返回Square对象
13     protected Shape factoryMethod(String aName) {
14         return new Square(aName + " (created by SquareFactory)");
15     }
16 }

测试:

 1 package org.masque.designpatterns.factorymethod.two;
 2 /**
 3  * 
 4  * Description:
 5  * Test.java Create on 2014年7月10日 下午5:28:44 
 6  * @author [email protected]
 7  * @version 1.0
 8  * Copyright (c) 2014 Company,Inc. All Rights Reserved.
 9  */
10 public class Test {
11 
12     public static void main(String[] args) {
13         ShapeFactory sf1 = new SquareFactory();
14         ShapeFactory sf2 = new CircleFactory();
15         sf1.anOperation("Shape one");
16         sf2.anOperation("Shape two");
17     }
18 }

The current shape is: Shape one (created by SquareFactory)
It will draw a square.
It will erase a square.
The current shape is: Shape two (created by CircleFactory)
It will draw a circle.
It will erase a circle.

这样做能够避免修改原有的代码,缺点是:代码量增加了不少!!!

你可能感兴趣的:(factory)