• 软件设计模式6


    原型模式

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

    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遍历模式。

    带Prototype Manager的原型模式:

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

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

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

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

    代码实现如下:

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

    //

    #include "stdafx.h"

    #include<iostream>

    #include<vector>

    #include<assert.h>

    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[1];

    name[0] = '';

    }

    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<<"ResumeA name : "<<name<<endl;

    }

    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[1];

    name[0] = '';

    }

    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<<"ResumeB name : "<<name<<endl;

    }

    class ResumeManager

    {

    private:

    vector<Resume *> mResume;

    public:

    ResumeManager()

    {

    }

    void add(Resume * resume)

    {

    mResume.push_back(resume);

    }

    Resume * get(int index) const

    {

    assert(index>=0 && index<mResume.size());

    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;

    }

  • 相关阅读:
    Quick Start WCF 4.0 RESTful Service Setup
    (面向c#开发人员) 编写javascript的好习惯六 for 表达式
    复习HTTP 14.4 HTTP Header AcceptLanguage
    visual studio 2010 中的 javascript 智能提示
    NewSlot and ReuseSlot
    复习HTTP 14.1 HTTP Header Accept (RFC 2616)
    (面向c#开发人员) 编写javascript的好习惯九 匿名自执行函数
    JQuery Template Engine 简介 1
    jquery 教程
    微博划词转发
  • 原文地址:https://www.cnblogs.com/zl00/p/14224598.html
Copyright © 2020-2023  润新知