6.原型模式

1.说明
请参阅本文第一章
2.原型模式说明
原型模式:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象
通俗说话:创建一个和原来一模一样的对象,减少代码中使用动态来new的方式,并且不用知道任何的创建细节
3.UML
这里写图片描述
4.代码

 //个人信息类,通过不同的数据类型来测试原型模式中的复制状况
 //当原型中的为指针类型时,为浅复制,其余均为深复制,且会调用复制构造函数

 //BaseClone.h 原型基类接口
#ifndef __BASECLONE_H
#define __BASECLONE_H

#include <iostream>
#include <string>
class CBaseClone
{
public:
    virtual CBaseClone* Clone(){}
};
#endif 

//个人信息类
#ifndef __WORKEXPRIENCE_H
#define __WORKEXPRIENCE_H

#include <iostream>

class CWorkExprience
{
public:
    void SetInfo(std::string time, std::string company)
    {
        m_time = time;
        m_company = company;
    }
    std::string GetTime()
    {
        return m_time;
    }
    std::string GetCompany()
    {
        return m_company;
    }
private:
    std::string m_time;
    std::string m_company;
};

#endif

//原型模式构造类

#ifndef __RESUME_H
#define __RESUME_H

#include <vector>
#include "BaseClone.h"
#include "WorkExprience.h"


//在原型模式中当 为值类型,类类型,复合类型(vector)均为 深复制
//指针类型为浅复制  


class CResume:public CBaseClone
{
public:
    CResume()
    {

    }
    /*CResume(CResume& par)
    {
        std::cout<<"this is nothing."<<std::endl;
    }*/
    void SetResumeName(std::string resuname)
    {
        m_resumename = resuname;
    }
    void SetInfo(std::string name, std::string sex)
    {
        m_name = name;
        m_sex = sex;        
    }
    //深浅复制测试区
    void AddWorkExprience(std::string time, std::string company)
    {
        m_exprience.SetInfo(time, company);
    }
    void Add_set(std::string time, std::string company)
    {
        CWorkExprience exper;
        exper.SetInfo(time, company);
        m_exset.push_back(exper);
    }
    void Set_mon(int* p)
    {
        if (p != NULL)
            m_mon = p;
    }
    //测试结束
    void Display()
    {
        std::cout<<"resuname:"<<m_resumename.c_str()<<"  "<<m_name.c_str()<<"  "<<m_sex.c_str()<<std::endl;
        std::cout<<m_exprience.GetTime()<<"  "<<m_exprience.GetCompany()<<std::endl;

        std::cout<<"setInfo:"<<std::endl;
        for (std::vector<CWorkExprience>::iterator it=m_exset.begin(); it!=m_exset.end(); it++)
        {
                std::cout<<it->GetTime()<<"  "<<it->GetCompany()<<std::endl;
        }
        std::cout<<"set endl"<<std::endl;
        std::cout<<"point test "<<*m_mon<< std::endl;        
    }
    CResume* Clone()
    {
        return new CResume(*this);              //此处会调用构造函数
    }
private:
    std::string m_resumename;
    std::string m_name;
    std::string m_sex;
    //std::vector<>
    CWorkExprience m_exprience;
    std::vector<CWorkExprience> m_exset;        //集合 用于测试clone时 是深复制还是浅复制
    int* m_mon;
};

#endif


//客户端调用
#include <iostream>
#include <string>
#include "Resume.h"
#include "BaseClone.h"

int main(void)
{   
    CResume* resume = new CResume();
    resume->SetResumeName("zp resume");
    resume->SetInfo("ZP", "BOY");
    resume->AddWorkExprience("2014-2015", "GHCA");
    resume->Add_set("1990-1995", "CHANGHONG");
    resume->Add_set("1996-1999", "GUANZI");
    resume->Add_set("2000-2003", "YILONG");
    int* mon = new int(20);
    resume->Set_mon(mon);

    std::cout<<"Init info"<<std::endl;
    resume->Display();

    std::cout<<std::endl<<std::endl;
    std::cout<<"light info"<<std::endl;
    CResume* light = (CResume* )resume->Clone();
    light->SetResumeName("Light Resume");
    light->AddWorkExprience("2013-2014", "SIFANG");
    light->Add_set("2004-2009", "LANGZHONG");
    light->Add_set("2009-2012", "CHENGDU");
    *mon = 30;
    light->Set_mon(mon);
    light->Display();

    std::cout<<std::endl<<std::endl<<std::endl; 
    std::cout<<"zp info"<<std::endl;
    resume->Display();

    return 0;   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值