您的位置:首页 > 其它

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

2014-07-22 11:44 411 查看


先看下一个简单的实现:

package org.masque.designpatterns.factorymethod.one;
/**
*
* Description: Sample子类的标示
* BeanEm.java Create on 2014年7月11日 下午2:37:58
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public enum BeanEm {

ONE,

TWO,

THREE;
}


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

public Sample(){
System.out.println(this);
}
}


package org.masque.designpatterns.factorymethod.one;
/**
*
* Description:子类A
* SampleA.java Create on 2014年7月11日 下午3:56:32
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class SampleA extends Sample{

public SampleA(){
}
}


package org.masque.designpatterns.factorymethod.one;
/**
*
* Description:子类B
* SampleB.java Create on 2014年7月11日 下午3:56:32
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class SampleBextends Sample{

public SampleB(){
}
}


package org.masque.designpatterns.factorymethod.one;

/**
*
* Description: BeanFactory.java Create on 2014年7月11日 下午2:38:29
*
* @author masque.java@outlook.com
* @version 1.0 Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class BeanFactory {

public static Sample createSample(BeanEm em) {
switch (em) {
case ONE:
return new SampleA();
case TWO:
return new SampleB();
default:
return null;
}
}
}


package org.masque.designpatterns.factorymethod.one;
/**
*
* Description:测试
* Test.java Create on 2014年7月11日 下午3:56:59
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class Test {

public static void main(String[] args) {
BeanFactory.createSample(BeanEm.ONE);
BeanFactory.createSample(BeanEm.TWO);
}
}






先定义一个抽象的父类

package org.masque.designpatterns.factorymethod.two;

/**
*
* Description: Shape.java Create on 2014年7月10日 下午4:53:27
*
* @author masque.java@outlook.com
* @version 1.0 Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public abstract class Shape {
// 勾画shape
public abstract void draw();

// 擦去 shape
public abstract void erase();

public String name;

public Shape(String name) {
this.name = name;
}
}


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

package org.masque.designpatterns.factorymethod.two;

public abstract class ShapeFactory {
protected abstract Shape factoryMethod(String aName);

// 在anOperation中定义Shape的一系列行为
public void anOperation(String aName) {
Shape s = factoryMethod(aName);
System.out.println("The current shape is: " + s.name);
s.draw();
s.erase();
}
}


子类一:

package org.masque.designpatterns.factorymethod.two;
/**
*
* Description:
* Circle.java Create on 2014年7月10日 下午5:28:06
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class Circle extends Shape {

public void draw() {
System.out.println("It will draw a circle.");
}

public void erase() {
System.out.println("It will erase a circle.");
}

// 构造函数
public Circle(String name) {
super(name);
}

}


package org.masque.designpatterns.factorymethod.two;
/**
*
* Description:
* CircleFactory.java Create on 2014年7月10日 下午5:28:18
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class CircleFactory extends ShapeFactory {
// 重载factoryMethod方法,返回Circle对象
protected Shape factoryMethod(String aName) {
return new Circle(aName + " (created by CircleFactory)");
}
}


子类二:

package org.masque.designpatterns.factorymethod.two;
/**
*
* Description:
* Square.java Create on 2014年7月10日 下午5:28:25
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class Square extends Shape {
public void draw() {
System.out.println("It will draw a square.");
}

public void erase() {
System.out.println("It will erase a square.");
}

// 构造函数
public Square(String name) {
super(name);
}
}


package org.masque.designpatterns.factorymethod.two;
/**
*
* Description:
* SquareFactory.java Create on 2014年7月10日 下午5:28:31
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class SquareFactory extends ShapeFactory {

// 重载factoryMethod方法,返回Square对象
protected Shape factoryMethod(String aName) {
return new Square(aName + " (created by SquareFactory)");
}
}


测试:

package org.masque.designpatterns.factorymethod.two;
/**
*
* Description:
* Test.java Create on 2014年7月10日 下午5:28:44
* @author masque.java@outlook.com
* @version 1.0
* Copyright (c) 2014 Company,Inc. All Rights Reserved.
*/
public class Test {

public static void main(String[] args) {
ShapeFactory sf1 = new SquareFactory();
ShapeFactory sf2 = new CircleFactory();
sf1.anOperation("Shape one");
sf2.anOperation("Shape two");
}
}


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.

这样做能够避免修改原有的代码,缺点是:代码量增加了不少!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: