C++设计模式

【C++设计模式】原型模式 浅拷贝

2021-03-10  本文已影响0人  小鱼号的代码日记
/* 小鱼号的代码日志
 * 设计模式
 * 原型模式
 * 用原型实例指定创建对象的种类,并且
 * 通过拷贝这些原型,创建新的对象
 * 一个对象在创建另外一个可定制的对象
 * 无需知道创建的细节
 * 克隆羊
 */

#include<iostream>
#include<list>
using namespace std;

class Animal
{
public:
    Animal(string name,int age)
    {
        m_name = name;
        m_age = age;
    }
    Animal(const Animal& animal)
    {
        m_name = animal.m_name;
        m_age = animal.m_age;
    }
    void showInfo()
    {
        cout << "name:" << m_name << " age:" << m_age << endl;
    }
    virtual Animal* clone() = 0;
protected:
    string m_name;
    int m_age;
};

class Sheep:public Animal
{
public:
    Sheep(string name,int age) :Animal(name,age)
    {
    }
    Sheep(const Sheep& animal) :Animal(animal)
    {
    }
    Animal* clone()
    {
        return new Sheep(*this);
    }
};

void testPrototype()
{
    cout << "prorotype patterns" << endl;
    Animal* sheep = new Sheep("tom",12);
    sheep->showInfo();
    Animal* cloneSheep = sheep->clone();
    cloneSheep->showInfo();
}
上一篇 下一篇

猜你喜欢

热点阅读