//Prototype.h
#ifndef _PROTOTYPE_H_
#define _PROTOTYPE_H_
class Prototype{
public:
virtual ~Prototype();
virtual Prototype* Clone() const = 0;
protected:
Prototype();
private:
};
class ConcretePrototype:public Prototype{
public:
ConcretePrototype();
ConcretePrototype(const ConcretePrototype& cp);
~ConcretePrototype();
Prototype* Clone() const;
protected:
private:
};
#endif //~_PROTOTYPE_H_
//Prototype.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
Prototype::Prototype(){
}
Prototype::~Prototype(){
}
Prototype* Prototype::Clone() const{
return 0;
}
ConcretePrototype::ConcretePrototype(){
}
ConcretePrototype::~ConcretePrototype(){
}
ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp){
cout<<"ConcretePrototype copy ..."<<endl;
}
Prototype* ConcretePrototype::Clone() const{
return new ConcretePrototype(*this);
}
//main.cpp
#include "Prototype.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[]){
Prototype* p = new ConcretePrototype();
Prototype* p1 = p->Clone();
return 0;
}
namespace Prototype_DesignPattern
{
using System;
// Objects which are to work as prototypes must be based on classes which
// are derived from the abstract prototype class
abstract class AbstractPrototype
{
abstract public AbstractPrototype CloneYourself();
}
// This is a sample object
class MyPrototype : AbstractPrototype
{
override public AbstractPrototype CloneYourself()
{
return ((AbstractPrototype)MemberwiseClone());
}
// lots of other functions go here!
}
// This is the client piece of code which instantiate objects
// based on a prototype.
class Demo
{
private AbstractPrototype internalPrototype;
public void SetPrototype(AbstractPrototype thePrototype)
{
internalPrototype = thePrototype;
}
public void SomeImportantOperation()
{
// During Some important operation, imagine we need
// to instantiate an object - but we do not know which. We use
// the predefined prototype object, and ask it to clone itself.
AbstractPrototype x;
x = internalPrototype.CloneYourself();
// now we have two instances of the class which as as a prototype
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
Demo demo = new Demo();
MyPrototype clientPrototype = new MyPrototype();
demo.SetPrototype(clientPrototype);
demo.SomeImportantOperation();
return 0;
}
}
}
using System;
class ShallowCopy : ICloneable
{
public int[] v = {1,2,3};
public Object Clone()
{
return this.MemberwiseClone();
}
public void Display()
{
foreach(int i in v)
Console.Write( i + ", ");
Console.WriteLine();
}
}
class Client
{
public static void Main()
{
ShallowCopy sc1 = new ShallowCopy();
ShallowCopy sc2 = (ShallowCopy)sc1.Clone();
sc1.v[0] = 9;
sc1.Display();
sc2.Display();
}
}
using System;
class DeepCopy : ICloneable
{
public int[] v = {1,2,3};
// 默认构造函数
public DeepCopy()
{
}
// 供Clone方法调用的私有构造函数
private DeepCopy(int[] v)
{
this.v = (int[])v.Clone();
}
public Object Clone()
{
// 构造一个新的DeepCopy对象,构造参数为
// 原有对象中使用的 v
return new DeepCopy(this.v);
}
public void Display()
{
foreach(int i in v)
Console.Write( i + ", ");
Console.WriteLine();
}
}
class Client
{
public static void Main()
{
DeepCopy dc1 = new DeepCopy();
DeepCopy dc2 = (DeepCopy)dc1.Clone();
dc1.v[0] = 9;
dc1.Display();
dc2.Display();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有