【设计模式】原型模式

原型模式,如其名称,核心就是要实现一个原型类,该类支持clone操作,从而客户可以从原型克隆出具体的类。

其效果主要有如下:

可以动态增加和删除产品。这个就是通过clone代替new等操作,也避免了客户与具体类打交道。

通过改变对象的值,指定新的对象。clone出一个新对象后可以修改其参数改变对象,如果参数较多,可以提供类似initialize方法。

减少creator类,减少子类数目。客户看起来直接从一个原型克隆出一个新的对象,而不是跟工厂方法打交道,因而少了一个类层次。

动态配置应用,这个真是没想明白。也没个例子,我想大致意思就是将原型编译成动态库等,初始化阶段构造原型对象,运行时从原型克隆出新的类吧。

写了个简单demo,供参考:

/**
 * @file test_prototype.cpp
 * @author itegel
 * @date 2013/09/18 15:15:26
 * @brief 
 *  
 **/

#include <iostream>
#include <string>
#include <map>
using namespace std;

class Prototype{
public:
    Prototype(){_myself = "Prototype";}

    ~Prototype(){}
    void ShowMyself(){
        cout<<"I am "<<_myself<<endl;
    }
    void SetMyself(string str){
        _myself = str;
    }
    virtual Prototype * Clone() = 0;
private:
    string _myself;
};

//Colour prototype for all colours
class Colour:public Prototype{
public:
    Colour(){
        SetRGB(0,0,0);
        SetMyself("Colour");
    }
    ~Colour(){}
    
    void SetRGB(int r, int g, int b){
        _R = r;
        _G = g;
        _B = b;
    }

    int GetR() const {return _R;}
    int GetG() const {return _G;}
    int GetB() const {return _B;}

    void Print(){
        cout<<"R:G:B=>"<<_R<<":"<<_G<<":"<<_B<<endl;
    }
    
    Colour(const Colour& other){
        this->SetRGB(other.GetR(),other.GetG(), other.GetB());    
    }

    virtual Colour * Clone(){
        return new Colour(*this);
    }
private:
    int _R;
    int _G;
    int _B;

};

//Type prototype for all colours
class Type:public Prototype{
public:
    Type(){
        SetTypeStr("0,0;");
        SetMyself("Type");
    }
    ~Type(){}
    
    void SetTypeStr(string type){
        _type_str = type;
    }
    
    string GetTypeStr() const{return _type_str;}

    void Print(){
        cout<<"type:"<<_type_str<<endl;
    }
    
    Type(const Type& other){
        this->SetTypeStr(other.GetTypeStr());    
    }

    virtual Type * Clone(){
        return new Type(*this);
    }
private:
    string _type_str;

};

class PrototypeMapManager{
public:
    PrototypeMapManager(){}
    ~PrototypeMapManager(){}
    int RegisterPrototype(string name, Prototype * prototype){
        map<string, Prototype *>::iterator iter = _prototype_map.find(name);
        if (iter != _prototype_map.end()){
            cout<<"prototype["<<name<<"] already exist!"<<endl;
            return -1;
        }
        _prototype_map.insert(pair<string, Prototype *>(name, prototype));
        return 0;
    }

    Prototype * GetPrototype(string name){
        map<string, Prototype *>::iterator iter = _prototype_map.find(name);
        
        if (iter == _prototype_map.end()){
            cout<<"prototype["<<name<<"] not find!"<<endl;
            return NULL;
        } else {
            return iter->second;
        }        
    }


private:
    map<string, Prototype *> _prototype_map;
};



//client
int main(){
    
    //test simple prototype
    Colour * prototype = new Colour();
    cout<<"1. prototype:"<<endl;
    prototype->Print();    
    Colour * blue = prototype->Clone();
    cout<<"2. after cloned:"<<endl;
    blue->Print();
    blue->SetRGB(0,0,255);
    cout<<"3. after set property:"<<endl;
    blue->Print();
    delete prototype;

    Type * type_prototype = new Type();
    Type * line = type_prototype->Clone();
    line->SetTypeStr("0,0;99,99;");
    cout<<"4. line:"<<endl;
    line->Print();
    
    //test dynamic produce objects
    cout<<endl;
    cout<<"DYNAMIC produce TEST"<<endl;
    PrototypeMapManager ProtoMgr;
    Colour * colour_prototype = new Colour();
    ProtoMgr.RegisterPrototype("Colour", colour_prototype);
    blue->SetMyself("Blue");
    ProtoMgr.RegisterPrototype("Blue", blue);
    ProtoMgr.RegisterPrototype("Type", type_prototype);
    line->SetMyself("Line");
    ProtoMgr.RegisterPrototype("Line", line);


    Prototype * clr_proto = ProtoMgr.GetPrototype("Colour");
    clr_proto->ShowMyself();
    Prototype * blue_proto = ProtoMgr.GetPrototype("Blue");
    blue_proto->ShowMyself();
    Prototype * red_proto = blue_proto->Clone();
    red_proto->SetMyself("Red");
    red_proto->ShowMyself();

    Prototype * line_proto = ProtoMgr.GetPrototype("Line");
    line_proto->ShowMyself();
 
    
    return 0;
}


/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
执行结果如下:

1. prototype:
R:G:B=>0:0:0
2. after cloned:
R:G:B=>0:0:0
3. after set property:
R:G:B=>0:0:255
4. line:
type:0,0;99,99;

DYNAMIC produce TEST
I am Colour
I am Blue
I am Red
I am Line

prototype比较麻烦的一点就是clone操作的定义有时候不是那么容易的。涉及到深浅拷贝等问题。具体网上有很多讨论,这里就不抄袭了。对于简单类这个其实也不是什么困难事。

原型模式是一种创建型设计模式,它提供了一种创建复杂对象的方式。这种模式允许你通过创建一个现有的对象(称为“原型”)并复制该对象来创建新的对象实例。 ### Java 设计模式 - 原型模式简介 #### 定义与用途 原型模式允许通过拷贝现有对象快速创建新对象,并在新对象上应用必要的修改。这种方式尤其适用于需要大量创建相似对象的情况,而无需编写大量的重复代码。原型模式使得创建过程更加高效、易于管理和维护。 #### 结构组成 原型模式由以下几个关键部分组成: 1. **`Prototype` 接口**:定义了一个对象创建的标准接口,所有具体的对象都应该实现这个接口。这通常包含一个 `clone()` 方法,用于生成对象的一个副本。 2. **具体原型**:实现了 `Prototype` 接口的具体对象。每个对象都可以有自己的克隆实现策略。 3. **克隆工厂**(可选):提供了一个统一的接口来请求克隆,这可以进一步封装克隆的具体实现细节。 4. **克隆者(Clonee)**:实际负责克隆工作的内部类或方法,它调用具体的对象实现 `clone()` 方法来生成一个副本。 #### 实现步骤 下面是一个简单的原型模式实现例子: ```java public interface Prototype { Prototype clone(); } public class ConcretePrototype implements Prototype { private String name; public ConcretePrototype(String name) { this.name = name; } @Override public Prototype clone() { return new ConcretePrototype(this.name); } // 其他属性和方法... } ``` 在这个例子中,`ConcretePrototype` 类实现了 `Prototype` 接口,并提供了一个 `clone()` 方法来生成一个新的 `ConcretePrototype` 对象,其名字与原始对象相同。 #### 应用场景 原型模式常见于游戏开发、数据库表单生成、配置文件管理等领域。例如,在构建游戏时,你可以创建一个游戏角色的基本模板,然后根据玩家的选择克隆出特定的角色,这样可以避免重复的代码和提高代码的复用性。 --- ### 相关问题: 1. **如何选择合适的对象作为原型模式的起点?** - 要点在于选择那些能够代表一类对象并且具备基本结构的对象作为原型。 2. **原型模式和浅拷贝、深拷贝的区别是什么?** - 浅拷贝仅复制对象的引用或浅层次的数据结构;而深拷贝会递归地复制整个对象树及其所有嵌套对象。 3. **在哪些情况下不适合使用原型模式?** - 当对象的创建非常简单,不需要复杂的构造逻辑时;当克隆操作成本极高,如涉及大量资源消耗或计算密集时;或者是对安全性有极高标准需求的应用场合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值