[原创]设计模型探索J-prototype parttern之如法炮制
原型模式
原型模式是在已指定对象的基础上,然后通过拷贝这些原型对象创建新的对象。
原型模式有两个组成部份
1.抽象原型
2.具体原型
例子:
1.抽象原型
3.具体原型2
4.测试类
总结:原型模式在生成复杂对象比较苦难的环境中比较适用,通过克隆已有对象来实现创建新的对象,节省了时间和空间。
原型模式是在已指定对象的基础上,然后通过拷贝这些原型对象创建新的对象。
原型模式有两个组成部份
1.抽象原型
2.具体原型
例子:
1.抽象原型
package
prototype;
/** */ /**
* <ul>
* <li>Title:[Prototype]</li>
* <li>Description: [抽象原型]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public interface Prototype
{
/** *//**
* <ul>
* <li>Description:[原型接口声明]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
* @throws CloneNotSupportedException
*/
public Object cloneMe() throws CloneNotSupportedException;
}
2.具体原型1/** */ /**
* <ul>
* <li>Title:[Prototype]</li>
* <li>Description: [抽象原型]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public interface Prototype
{
/** *//**
* <ul>
* <li>Description:[原型接口声明]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @return
* @throws CloneNotSupportedException
*/
public Object cloneMe() throws CloneNotSupportedException;
}
package
prototype;
/** */ /**
* <ul>
* <li>Title:[Circle]</li>
* <li>Description: [圆类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class Circle implements Prototype, Cloneable
{
/** *//** 半径 */
private double radius;
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param radius
*/
public Circle(double radius)
{
this.radius = radius;
}
/**//* (non-Javadoc)
* @see prototype.Prototype#cloneMe()
*/
public Object cloneMe() throws CloneNotSupportedException
{
Circle obj = (Circle)clone();
return obj;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
}
/** */ /**
* <ul>
* <li>Title:[Circle]</li>
* <li>Description: [圆类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
public class Circle implements Prototype, Cloneable
{
/** *//** 半径 */
private double radius;
/** *//**
* <ul>
* <li>Description:[构造方法]</li>
* <ul>
*
* @param radius
*/
public Circle(double radius)
{
this.radius = radius;
}
/**//* (non-Javadoc)
* @see prototype.Prototype#cloneMe()
*/
public Object cloneMe() throws CloneNotSupportedException
{
Circle obj = (Circle)clone();
return obj;
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
}
3.具体原型2
package
prototype;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/** */ /**
* <ul>
* <li>Title:[Triangle]</li>
* <li>Description: [三角形,用序列化的方式复制]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
@SuppressWarnings( " serial " )
public class Triangle implements Prototype, Serializable
{
/** *//** a边 */
private double a;
/** *//** b边 */
private double b;
/** *//** c边 */
private double c;
/** *//**
* <ul>
* <li>Description:[测试信息]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void getDebugInfo()
{
StringBuffer debuginfo = new StringBuffer();
debuginfo.append("原实例三边信息-> ");
debuginfo.append("a:").append(a).append(", ");
debuginfo.append("b:").append(a).append(", ");
debuginfo.append("c:").append(a);
System.out.println(debuginfo);
}
/**//* (non-Javadoc)
* @see prototype.Prototype#cloneMe()
*/
public Object cloneMe() throws CloneNotSupportedException
{
Object obj = null;
try
{
ByteArrayOutputStream outOne = new ByteArrayOutputStream();
ObjectOutputStream outTwo = new ObjectOutputStream(outOne);
outTwo.writeObject(this);
ByteArrayInputStream inOne = new ByteArrayInputStream(outOne.toByteArray());
ObjectInputStream inTwo = new ObjectInputStream(inOne);
obj = inTwo.readObject();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return obj;
}
public double getA()
{
return a;
}
public void setA(double a)
{
this.a = a;
}
public double getB()
{
return b;
}
public void setB(double b)
{
this.b = b;
}
public double getC()
{
return c;
}
public void setC(double c)
{
this.c = c;
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/** */ /**
* <ul>
* <li>Title:[Triangle]</li>
* <li>Description: [三角形,用序列化的方式复制]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*
* @version 1.0
*/
@SuppressWarnings( " serial " )
public class Triangle implements Prototype, Serializable
{
/** *//** a边 */
private double a;
/** *//** b边 */
private double b;
/** *//** c边 */
private double c;
/** *//**
* <ul>
* <li>Description:[测试信息]</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
*/
public void getDebugInfo()
{
StringBuffer debuginfo = new StringBuffer();
debuginfo.append("原实例三边信息-> ");
debuginfo.append("a:").append(a).append(", ");
debuginfo.append("b:").append(a).append(", ");
debuginfo.append("c:").append(a);
System.out.println(debuginfo);
}
/**//* (non-Javadoc)
* @see prototype.Prototype#cloneMe()
*/
public Object cloneMe() throws CloneNotSupportedException
{
Object obj = null;
try
{
ByteArrayOutputStream outOne = new ByteArrayOutputStream();
ObjectOutputStream outTwo = new ObjectOutputStream(outOne);
outTwo.writeObject(this);
ByteArrayInputStream inOne = new ByteArrayInputStream(outOne.toByteArray());
ObjectInputStream inTwo = new ObjectInputStream(inOne);
obj = inTwo.readObject();
}
catch(Exception ex)
{
ex.printStackTrace();
}
return obj;
}
public double getA()
{
return a;
}
public void setA(double a)
{
this.a = a;
}
public double getB()
{
return b;
}
public void setB(double b)
{
this.b = b;
}
public double getC()
{
return c;
}
public void setC(double c)
{
this.c = c;
}
}
4.测试类
package
prototype;
/** */ /**
* <ul>
* <li>Title:[Test]</li>
* <li>Description: [测试类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Test
{
public static void main(String[] args) throws CloneNotSupportedException
{
Circle circle = new Circle(10);
System.out.println("原实例半径: " + circle.getRadius());
Circle copyCircle = (Circle)circle.cloneMe();
System.out.println("复制例半径: " + copyCircle.getRadius());
copyCircle.setRadius(20);
System.out.println("原实例半径: " + circle.getRadius());
System.out.println("复制例半径: " + copyCircle.getRadius());
Triangle triangle = new Triangle();
triangle.setA(10);
triangle.setB(20);
triangle.setC(30);
triangle.getDebugInfo();
Triangle copyTriangle = (Triangle)triangle.cloneMe();
copyTriangle.getDebugInfo();
copyTriangle.setA(11);
copyTriangle.setB(21);
copyTriangle.setC(31);
copyTriangle.getDebugInfo();
}
}
/** */ /**
* <ul>
* <li>Title:[Test]</li>
* <li>Description: [测试类]</li>
* <li>Copyright 2009 Upengs Co., Ltd.</li>
* <li>All right reserved.</li>
* <li>Created by [Huyvanpull] [2011-8-2]</li>
* <li>Midified by [modifier] [modified time]</li>
* </ul>
* @version 1.0
*/
public class Test
{
public static void main(String[] args) throws CloneNotSupportedException
{
Circle circle = new Circle(10);
System.out.println("原实例半径: " + circle.getRadius());
Circle copyCircle = (Circle)circle.cloneMe();
System.out.println("复制例半径: " + copyCircle.getRadius());
copyCircle.setRadius(20);
System.out.println("原实例半径: " + circle.getRadius());
System.out.println("复制例半径: " + copyCircle.getRadius());
Triangle triangle = new Triangle();
triangle.setA(10);
triangle.setB(20);
triangle.setC(30);
triangle.getDebugInfo();
Triangle copyTriangle = (Triangle)triangle.cloneMe();
copyTriangle.getDebugInfo();
copyTriangle.setA(11);
copyTriangle.setB(21);
copyTriangle.setC(31);
copyTriangle.getDebugInfo();
}
}
总结:原型模式在生成复杂对象比较苦难的环境中比较适用,通过克隆已有对象来实现创建新的对象,节省了时间和空间。