原型模式

模式简介

当一个对象需要完全拷贝另一个对象的状态时,需要使用这个模式。此时,拷贝的新对象和原来的对象拥有同样的行为状态。这么做的优势是,新建对象的时候,我们不需要人工把新对象改变到原来对象的状态,而是复制函数帮我们处理完毕了。

这个模式一般用在那些需要克隆有特定状态的对象。比如一个对象当前存储了某些特定的数值,这些值非常多而且复杂,那么此时就可以使用这个模式了。C++中通过拷贝构造函数来实现。

模式实现

需要有一个原型类的纯虚基类提供克隆函数,之后具体的子类继承这个纯虚基类,并实现自己的Clone函数。

UML类图

代码实例

#include <iostream>
#include <memory>
#include <string>

class Prototype {
public:
    virtual std::shared_ptr<Prototype> Clone() = 0;
    // 这2个纯虚函数仅是为了显示用的
    virtual std::string getName() const = 0;
    virtual int getScore() const = 0;
};

class ConcretePrototype : public Prototype {
public:
    ConcretePrototype() = default;
    ConcretePrototype(ConcretePrototype& type) {
        m_strName = type.m_strName;
        m_iScore = type.m_iScore;
    }
    std::shared_ptr<Prototype> Clone() override {
        return std::make_shared<ConcretePrototype>(*this);
    }

    void setName(std::string name) {
        m_strName = std::move(name);
    }

    void setScore(int score) {
        m_iScore = score;
    }

    std::string getName() const override {
        return m_strName;
    }

    int getScore() const override {
        return m_iScore;
    }

private:
    std::string m_strName;
    int m_iScore{0};
};

int main() {
    std::shared_ptr<Prototype> ptr;
    ConcretePrototype cpt1, cpt2;
    cpt1.setName("Tom");
    cpt1.setScore(100);
    ptr = cpt1.Clone();
    std::cout << ptr->getName() << ", " << ptr->getScore() << std::endl;
    cpt2.setName("Lily");
    cpt2.setScore(97);
    ptr = cpt2.Clone();
    std::cout << ptr->getName() << ", " << ptr->getScore() << std::endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值