设计模式(创建型)之原型模式
????????GOF?給原型模式?的定義為:用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過拷貝這些原型創(chuàng)建新的對(duì)象。
????????在?C++和Java?中都提供了clone()方法來實(shí)現(xiàn)對(duì)象的克隆,但是在Java中必須要實(shí)現(xiàn)Cloneable這個(gè)接口。
????????通過給出一個(gè)原型對(duì)象來指明所要?jiǎng)?chuàng)建的對(duì)象的類型,然后用復(fù)制這個(gè)原型對(duì)象的方法創(chuàng)建出更多同類型的對(duì)象。原始模型模式允許動(dòng)態(tài)的增加或減少產(chǎn)品類,產(chǎn)品類不需要非得有任何事先確定的等級(jí)結(jié)構(gòu),原始模型模式適用于任何的等級(jí)結(jié)構(gòu)。缺點(diǎn)是每一個(gè)類都必須配備一個(gè)克隆方法。
一、組成:
????????1)?客戶角色:讓一個(gè)原型克隆自己來得到一個(gè)新對(duì)象。
????????2)?抽象原型角色:實(shí)現(xiàn)了自己的clone?方法,扮演這種角色的類通常是抽象類,且它具有許多具體的子類。
????????3)?具體原型角色:被復(fù)制的對(duì)象,為抽象原型角色的具體子類。
二、UML類圖:
三、代碼實(shí)現(xiàn):
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;???
?}???
}?
//原型實(shí)現(xiàn)類
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());
?}
}
?
輸出結(jié)果:
1:ConcretePrototype1
2:ConcretePrototype1
3:ConcretePrototype2
4:ConcretePrototype2
?
C++:
Prototype.h:
/*
?*?Prototype.h
?*
?*??Created?on:?2013-6-23
?*??????Author:?yan?chao
?*/
#ifndef?PROTOTYPE_H_
#define?PROTOTYPE_H_
//?虛擬基類,所有原型的基類,提供Clone接口函數(shù)
class?Prototype
{
public:
?Prototype(){}
?virtual?~Prototype(){}
?virtual?Prototype*?Clone()?=?0;
};
//?派生自Prototype,實(shí)現(xiàn)Clone方法
class?ConcreatePrototype1?:?public?Prototype
{
public:
?ConcreatePrototype1();
?ConcreatePrototype1(const?ConcreatePrototype1&);
?virtual?~ConcreatePrototype1();
?virtual?Prototype*?Clone();
};
//?派生自Prototype,實(shí)現(xiàn)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;
}
?
?運(yùn)行結(jié)果:
construction?of?ConcreatePrototype1n
copy?construction?of?ConcreatePrototype1n
construction?of?ConcreatePrototype2n
copy?construction?of?ConcreatePrototype2n
destruction?of?ConcreatePrototype1n
destruction?of?ConcreatePrototype1n
destruction?of?ConcreatePrototype2n
destruction?of?ConcreatePrototype2n
?
?
轉(zhuǎn)載于:https://blog.51cto.com/flyingsnail/1227994
總結(jié)
以上是生活随笔為你收集整理的设计模式(创建型)之原型模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “棱镜”折射出了什么
- 下一篇: JAVA的23种设计模式