GOF 给原型模式 的定义为:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
在 C++和Java 中都提供了clone()方法来实现对象的克隆,但是在Java中必须要实现Cloneable这个接口。
通过给出一个原型对象来指明所要创建的对象的类型,然后用复制这个原型对象的方法创建出更多同类型的对象。原始模型模式允许动态的增加或减少产品类,产品类不需要非得有任何事先确定的等级结构,原始模型模式适用于任何的等级结构。缺点是每一个类都必须配备一个克隆方法。
一、组成:
1) 客户角色:让一个原型克隆自己来得到一个新对象。
2) 抽象原型角色:实现了自己的clone 方法,扮演这种角色的类通常是抽象类,且它具有许多具体的子类。 3) 具体原型角色:被复制的对象,为抽象原型角色的具体子类。二、UML类图:
三、代码实现:
JAVA:
Prototype.java:
//原型抽象类
public abstract class Prototype implements Cloneable{ String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; }public Object clone(){
Object object = null; try { object = super.clone(); } catch (CloneNotSupportedException exception) { System.err.println("AbstractPrototype is not Cloneable"); } return object; } }//原型实现类
class ConcretePrototype1 extends Prototype {public ConcretePrototype1() {
// TODO Auto-generated method stub setName("ConcretePrototype1"); }}class ConcretePrototype2 extends Prototype {
public ConcretePrototype2() {
// TODO Auto-generated method stub setName("ConcretePrototype2"); }}
testMain.java:
public class testMain {
public static void main(String[] args) throws CloneNotSupportedException {
Prototype pt1 = new ConcretePrototype1(); Prototype pt2 = (Prototype) pt1.clone(); Prototype pt3 = new ConcretePrototype2(); Prototype pt4 = (Prototype) pt3.clone(); System.out.println("1:"+pt1.getName()); System.out.println("2:"+pt2.getName()); System.out.println("3:"+pt3.getName()); System.out.println("4:"+pt4.getName());}
}
输出结果:
1:ConcretePrototype1
2:ConcretePrototype13:ConcretePrototype24:ConcretePrototype2
C++:
Prototype.h:
/*
* Prototype.h * * Created on: 2013-6-23 * Author: yan chao*/
#ifndef PROTOTYPE_H_
#define PROTOTYPE_H_// 虚拟基类,所有原型的基类,提供Clone接口函数
class Prototype{ public: Prototype(){} virtual ~Prototype(){}virtual Prototype* Clone() = 0;
};// 派生自Prototype,实现Clone方法
class ConcreatePrototype1 : public Prototype{ public: ConcreatePrototype1(); ConcreatePrototype1(const ConcreatePrototype1&); virtual ~ConcreatePrototype1();virtual Prototype* Clone();
};// 派生自Prototype,实现Clone方法
class ConcreatePrototype2 : public Prototype{ public: ConcreatePrototype2(); ConcreatePrototype2(const ConcreatePrototype2&); virtual ~ConcreatePrototype2();virtual Prototype* Clone();
}; #endif /* PROTOTYPE_H_ */
Prototype.cpp:
//============================================================================
// Name : Prototype.cpp// Author : yan chao// Version :// Copyright : Your copyright notice// Description : Hello World in C++, Ansi-style//============================================================================#include "Prototype.h"
#include <iostream>ConcreatePrototype1::ConcreatePrototype1()
{ std::cout << "construction of ConcreatePrototype1n" << std::endl;}ConcreatePrototype1::~ConcreatePrototype1()
{ std::cout << "destruction of ConcreatePrototype1n"<< std::endl;}ConcreatePrototype1::ConcreatePrototype1(const ConcreatePrototype1&)
{ std::cout << "copy construction of ConcreatePrototype1n"<< std::endl;}Prototype* ConcreatePrototype1::Clone()
{ return new ConcreatePrototype1(*this);}ConcreatePrototype2::ConcreatePrototype2()
{ std::cout << "construction of ConcreatePrototype2n"<< std::endl;}ConcreatePrototype2::~ConcreatePrototype2()
{ std::cout << "destruction of ConcreatePrototype2n"<< std::endl;}ConcreatePrototype2::ConcreatePrototype2(const ConcreatePrototype2&)
{ std::cout << "copy construction of ConcreatePrototype2n"<< std::endl;}Prototype* ConcreatePrototype2::Clone()
{ return new ConcreatePrototype2(*this);}
testMain.cpp:
/*
* testMain.cpp * * Created on: 2013-6-23 * Author: yan chao */#include "Prototype.h"
#include <stdlib.h>int main()
{ Prototype* pPrototype1 = new ConcreatePrototype1(); Prototype* pPrototype2 = pPrototype1->Clone();Prototype* pPrototype3 = new ConcreatePrototype2();
Prototype* pPrototype4 = pPrototype3->Clone();delete pPrototype1;
delete pPrototype2; delete pPrototype3; delete pPrototype4;system("pause");
return 0;
}
运行结果:
construction of ConcreatePrototype1n
copy construction of ConcreatePrototype1nconstruction of ConcreatePrototype2ncopy construction of ConcreatePrototype2ndestruction of ConcreatePrototype1ndestruction of ConcreatePrototype1ndestruction of ConcreatePrototype2ndestruction of ConcreatePrototype2n