原型法的基本思想 原型模式 原型模式-基本内容

原型模式是一种创建型设计模式,通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。

原型模式_原型模式 -基本内容

用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

Prototype原型模式是一种创建型设计模式,Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传给那个要发动创建的对象,这个要发动创建的对象通过请求原型对象拷贝它们自己来实施创建。

它主要面对的问题是:"某些结构复杂的对象"的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是他们却拥有比较稳定一致的接口。

因为Java中的提供clone()方法来实现对象的克隆,所以Prototype模式实现一下子变得很简单.

以勺子为例:

public abstract class AbstractSpoon implements Cloneable

{

String spoonName;

public void setSpoonName(String spoonName) {this.spoonName = spoonName;}

public String getSpoonName() {return this.spoonName;}

public Object clone()

{

Object object = null;

try {

object = super.clone();

} catch (CloneNotSupportedException exception) {

System.err.println("AbstractSpoon is not Cloneable");

}

return object;

}

}

有两个具体实现(ConcretePrototype):

public class SoupSpoon extends AbstractSpoon

{

public SoupSpoon()

{

setSpoonName("Soup Spoon");

}

}

调用Prototype模式很简单:

AbstractSpoon spoon = new SoupSpoon();

AbstractSpoon spoon = new SaladSpoon();

当然也可以结合工厂模式来创建AbstractSpoon实例。

在Java中Prototype模式变成clone()方法的使用,由于Java的纯洁的面向对象特性,使得在Java中使用设计模式变得很自然,两者已经几乎是浑然一体了。这反映在很多模式上,如Iterator遍历模式。

客户(Client)角色:客户端类向原型管理器提出创建对象的请求。

抽象原型(Prototype)角色:这是一个抽象角色,通常由一个C#接口或抽象类实现。此角色给出所有的具体原型类所需的接口。在C#中,抽象原型角色通常实现了ICloneable接口。

具体原型(Concrete Prototype)角色:被复制的对象。此角色需要实现抽象的原型角色所要求的接口。

原型管理器(Prototype Manager)角色:创建具体原型类的对象,并记录每一个被创建的对象。

代码实现如下:

/ CplusplusPrototype.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include

#include

#include

usingnamespace std;

//父类

class Resume

{

protected:

char *name;

public:

Resume() {}

virtual ~Resume() {}

virtual Resume* Clone() { return NULL; }

virtualvoid Set(char *n) {}

virtualvoid Show() {}

};

class ResumeA : public Resume

{

public:

ResumeA(constchar *str); //构造函数

ResumeA(const ResumeA &r); //拷贝构造函数

~ResumeA(); //析构函数

ResumeA* Clone(); //克隆,关键所在

void Show(); //显示内容

};

ResumeA::ResumeA(constchar *str)

{

if(str == NULL) {

name = newchar;

name = '';

}

else {

name = newchar[strlen(str)+1];

strcpy(name, str);

}

}

ResumeA::~ResumeA() { delete [] name;}

ResumeA::ResumeA(const ResumeA &r) {

name = newchar[strlen(r.name)+1];

strcpy(name, r.name);

}

ResumeA* ResumeA::Clone() {

returnnew ResumeA(*this);

}

void ResumeA::Show() {

cout

}

class ResumeB : public Resume

{

public:

ResumeB(constchar *str); //构造函数

ResumeB(const ResumeB &r); //拷贝构造函数

~ResumeB(); //析构函数

ResumeB* Clone(); //克隆,关键所在

void Show(); //显示内容

};

ResumeB::ResumeB(constchar *str)

{

if(str == NULL) {

name = newchar;

name = '';

}

else {

name = newchar[strlen(str)+1];

strcpy(name, str);

}

}

ResumeB::~ResumeB() { delete [] name;}

ResumeB::ResumeB(const ResumeB &r) {

name = newchar[strlen(r.name)+1];

strcpy(name, r.name);

}

ResumeB* ResumeB::Clone() {

returnnew ResumeB(*this);

}

void ResumeB::Show() {

cout

}

class ResumeManager

{

private:

vectormResume;

public:

ResumeManager()

{

}

void add(Resume * resume)

{

mResume.push_back(resume);

}

Resume * get(int index) const

{

assert(index>=0 && index

return mResume[index];

}

};

int _tmain(int argc, _TCHAR* argv[])

{

ResumeManager *manager = new ResumeManager();

Resume *r1 = new ResumeA("A");

Resume *r2 = new ResumeB("B");

manager->add(r1);

manager->add(r2);

manager->get(0)->Show();

manager->get(1)->Show();

Resume *r3 = manager->get(0)->Clone();

Resume *r4 = manager->get(1)->Clone();

//删除r1,r2

原型法的基本思想 原型模式 原型模式-基本内容

delete r1; delete r2;

r1 = r2 = NULL;

//深拷贝所以对r3,r4无影响

r3->Show(); r4->Show();

delete r3; delete r4;

r3 = r4 = NULL;

return 0;

}

  

爱华网本文地址 » http://www.aihuau.com/a/8103320103/56443.html

更多阅读

新奥法的基本原理 锚杆的作用

新奥法的基本原理新奥法的创始人之一,奥地利的L·米勒(L·Muller)教授根据多年来新奥法设计、施工与实践的经验教训、存在的问题与对策,于1978年总结出新奥法基本原则22 条。(1)隧道是以其自身的围岩来支护的,衬砌与围岩应紧密地贴合在一

黑格尔哲学的基本观点 促销模式基本概念与观点

  基本概念与观点   -促销模式   促销模式主要有三个维度:折扣幅度,一段时间内折扣的幅度;折扣幅度变化,是指在一段时间内促销是以固定幅度还是以变动幅度进行;促销频率,在一定时间内进行折扣促销的频率。   -感知价格差异(PPD,th

国际战略格局发展趋势 战略的基本思想与趋势4

      战略控制过程的基本要求:(1)适宜性。适宜性需要以公司既定的一系列评价标准为依据,这些标准中财务标准与发展标准至关重要。(2)可行性,这指战略是否适应企业的财力,人力或其它资源,技能,技术,诀窍和组织优势。(3)可接受性,是指战略要

声明:《原型法的基本思想 原型模式 原型模式-基本内容》为网友穿著布鞋走囼分享!如侵犯到您的合法权益请联系我们删除