设计模式(C++实例)

感言
本文很长,但文中近乎有十之八九只能算是我在拜读各位大神大牛们分享的作品的笔记,这里只是我第一遍学习,我知道过了几天我肯定又会把看过的东西给忘记,所以就索性把各位大牛们的东西整理到了一起,方便以后的学习。

1.(Strategy策略模式)
一个策略放到一个锦囊里。当用的时候,找到这个锦囊,从锦囊里拿出策略来使用。
注意:锦囊只是简单的装载和调用策略,锦囊里没有逻辑。策略会有更大的自主权,运行更多的逻辑。
策略接口

//IStrategy.h
#pragma once
class IStrategy
{
public:
    IStrategy(void);
    virtual ~IStrategy(void);
    virtual void Operator(void) = 0;
};

实际策略BackDoor

//BackDoor.h
#pragma once
#include "IStrategy.h"
class CBackDoor : public IStrategy
{
public:
    CBackDoor(void);
    ~CBackDoor(void);
    void Operator(void);
};
//BackDoor.cpp
#include "stdafx.h"
#include "BackDoor.h"
#include <iostream>
using std::cout;
using std::endl;
CBackDoor::CBackDoor(void)
{
}
CBackDoor::~CBackDoor(void)
{
}
void CBackDoor::Operator(void)
{
    cout << "找乔国老帮忙,让吴国太给孙权施压" << endl;
}

实际策略GivenGreenLigh

//GivenGreenLight.h
#pragma once
#include "IStrategy.h"
class GivenGreenLight : public IStrategy
{
public:
    GivenGreenLight(void);
    ~GivenGreenLight(void);
    void Operator(void);
};
//GivenGreenList.cpp
#include "stdafx.h"
#include "GivenGreenLight.h"
#include <iostream>
using std::cout;
using std::endl;
GivenGreenLight::GivenGreenLight(void)
{
}
GivenGreenLight::~GivenGreenLight(void)
{
}
void GivenGreenLight::Operator(void)
{
    cout << "求吴国太给绿灯,放行" << endl;
}

使用锦囊包装策略CContext

#pragma once
#include "stdafx.h"
#include "IStrategy.h"
class Context
{
   
public:
    Context(IStrategy *strategy);
    ~Context();
    void Operator(void);
private:
    IStrategy *m_strategy;
};
//Context.cpp
#include "stdafx.h"
#include "Context.h"
Context::Context(IStrategy *mStrategy)
{ 
    this->m_strategy = mStrategy;
}
Context::~Context(void)
{
    delete this->m_strategy;
}
void Context::Operator()
{
    this->m_strategy->Operator();
}

结果测试

//Strategy.cpp

#include "stdafx.h"
#include "BackDoor.h"
#include "GivenGreenLight.h"
#include "BlockEnemy.h"
#include "Context.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Context *p_context;
    p_context = new Context(new CBackDoor());
    p_context->Operator();
    p_context = new Context(new GivenGreenLight());
    p_context->Operator();
    system(“pause”);
    return 0;
}

代码结构图:
这里写图片描述
运行结果:
这里写图片描述
一个锦囊只能装一个妙计,可以有多个锦囊。属于对象行为型模式

2.(Proxy代理模式)
所谓代理,一看名字就知道这只是个中介而已,真实的执行者在代理的后面。
代理和实际执行者派生于共同的接口,代理拥有实际执行者的实例。代理的每一个函数(接口的实现函数),直接调用实际执行者的对应接口函数。
注意:代理只是简单的装载,然后调用实际执行者的函数
代理和实际执行者派生于共同的接口

//IKindWomen.h
#pragma once
class IKindWomen
{
public:
    IKindWomen(void) {};
    virtual ~IKindWomen(void) {};
    virtual void MakeEyesWithMan() = 0;
    virtual void HappyWithMan() = 0;
};

实际执行者的代理者

//WangPo.h
#pragma once
#include "IKindWomen.h"
class WangPo : public IKindWomen
{
public:
    WangPo(IKindWomen *pKindWomen);
    ~WangPo(void);
    void HappyWithMan(void);
    void MakeEyesWithMan(void);
private:
    IKindWomen *m_kindwomen;
};
//WangPo.cpp

#include "stdafx.h"
#include "WangPo.h"
WangPo::WangPo(IKindWomen *pIKindWomen)
{
    this->m_kindwomen = pIKindWomen;
}
WangPo::~WangPo(void)
{
    delete this->m_kindwomen;
}
void WangPo::HappyWithMan()
{
    this->m_kindwomen->HappyWithMan();
}
void WangPo::MakeEyesWithMan()
{
    this->m_kindwomen->MakeEyesWithMan();
}

实际执行者PanJinLian

//PanJinLian.h

#pragma once
#include "IKindWomen.h"
class PanJinLian : public IKindWomen
{
public:
    PanJinLian(void);
    ~PanJinLian(void);
    void HappyWithMan();
    void MakeEyesWithMan();
};
//PanJinLian.cpp

#include "stdafx.h"
#include "PanJinLian.h"
#include <iostream>
using std::cout;
using std::endl;

PanJinLian::PanJinLian(void)
{
}
PanJinLian::~PanJinLian(void)
{
}
void PanJinLian::HappyWithMan()
{
    cout << "潘金莲和男人做那个..." << endl;
}
void PanJinLian::MakeEyesWithMan(void)
{
    cout << "潘金莲抛媚眼" << endl;
}

实际执行者JiaShi

//JiaShi.h

#pragma once
#include "IKindWomen.h"
class JiaShi : public IKindWomen
{
public:
    JiaShi(void);
    ~JiaShi(void);
    void HappyWithMan();
    void MakeEyesWithMan();
};
//JiaShi.cpp

#include "stdafx.h"
#include "JiaShi.h"
#include <iostream>
using std::cout;
using std::endl;

JiaShi::JiaShi(void)
{
}
JiaShi::~JiaShi(void)
{
}
void JiaShi::HappyWithMan()
{
    cout << "贾氏和男人做那个..." << endl;
}
void JiaShi::MakeEyesWithMan(void)
{
    cout << "贾氏抛媚眼" << endl;
}

实际测试

// PoxyPatternDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "WangPo.h"
#include "PanJinLian.h"
#include "JiaShi.h"

void doPanJinLian()
{
    WangPo *pWangPo;
    pWangPo = new WangPo(new PanJinLian());
    pWangPo->MakeEyesWithMan();
    pWangPo->HappyWithMan();
    delete pWangPo;
}
void doJiaShi()
{
    WangPo *pWangPo;
    pWangPo = new WangPo(new JiaShi());
    pWangPo->MakeEyesWithMan();
    pWangPo->HappyWithMan();
    delete pWangPo;
}
int _tmain(int argc, _TCHAR* argv[])
{
    doPanJinLian();

    doJiaShi();
    system("pause");

    return 0;
}

代码结构图:
这里写图片描述
运行结果:
这里写图片描述
看起来代理模式的结构和策略模式类似,都是由一个类来装载接口的一个实例,策略模式是CContext来装载,代理模式是CWangPo来装载。CContext不是从IStrategy派生,所以不需要实现IStrategy接口函数,而CWangPo是从IKindWomen派生的所以CWangPo很清楚CPanJinLian和CJiaShi的接口函数。这就是代理,代理人知道被代理人能干的事情即函数,所以代理人可以成为中介。
代理模式可以很好的将前后端分开,实现了松散耦合。代理模式属于结构型模式

3.(Singleton单例模式)
单例模式顾名思义,就是在系统中只允许产生这个类的一个实例。
实例说明:很多大臣拜见的皇帝,只有一个。体现在面向对象方面,CEmperor定义一个静态指针,和一个静态函数,私有化构造函数、析构函数、构造函数复制、重载赋值语句。
注意:线程安全,采用互斥体的方式实现
用单例的方式实现Emperor,不论在使用过程中new多少次均只会有一个实例

//Emperor.h
#pragma once
#include <Windows.h>
#include <winnt.h>
#include <iostream>
using std::cout;
using std::endl;
using std::string;
class Emperor
{
public:
    static Emperor* getInstance();
    static void releaseInstance();
    void emperorInfo(void);
    void setEmperorTag(string tag);
private:
    Emperor(void);
    virtual ~Emperor(void);
    Emperor(const Emperor&);
    Emperor& operator=(const Emperor&);
    static Emperor *m_emperor;
    static HANDLE m_mutex;
    string m_emperor_tag;
    class Garbo
    {
    public:
        Garbo()
        {
            cout << "create garbo" << endl;
        }
        ~Garbo()
        {
            cout << "destrory garbo" << endl;
            getchar();
            if(NULL != m_emperor)
            {
                WaitForSingleObject(m_mutex, INFINITE);
                if(NULL != m_emperor)
                {
                    cout << "remove instance" << endl;
                    delete m_emperor;
                    m_emperor = NULL;
                }
                ReleaseMutex(m_mutex);
            }
            if(NULL != m_mutex)
            {
                cout << "delete mutex" << endl;
                CloseHandle(m_mutex);
                m_mutex = NULL;
            }
        }
    };
    static Garbo m_garbo;
};
//Emperor.cpp
#include "stdafx.h"
#include "Emperor.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
Emperor *Emperor::m_emperor = NULL;
HANDLE Emperor::m_mutex = CreateMutex(NULL, FALSE, NULL);
Emperor::Garbo Emperor::m_garbo;
Emperor::Emperor(void)
{
    cout << "create emperor instance" << endl;
}
Emperor::~Emperor(void)
{
    cout << "destroy emperor instance and release its resources" << endl;
}
void Emperor::emperorInfo(void)
{
    char msg_buf[50] = {
  0};
    sprintf_s(msg_buf, 50, "the emperor's name is (%s)", m_emperor_tag.c_str());
    string msg(msg_buf);
    cout << msg.c_str() << endl;
}
Emperor *Emperor::getInstance()
{
    if(NULL == m_emperor)
    {
        WaitForSingleObject(m_mutex, INFINITE);
        if(NULL == m_emperor)
            m_emperor = new Emperor();
        ReleaseMutex(m_mutex);
    }
    return m_emperor;
}
void Emperor::releaseInstance()
{
    if(NULL != m_emperor)
    {
        WaitForSingleObject(m_mutex, INFINITE);
        if(NULL != m_emperor)
        {
            delete m_emperor;
            m_emperor = NULL;
        }
        ReleaseMutex(m_mutex);
    }
}
void Emperor::setEmperorTag(string tag)
{
    m_emperor_tag = tag;
}

实例测试

// SingletoPatternDemo.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "Emperor.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Emperor *pEmperor1 = Emperor::getInstance();
    pEmperor1->setEmperorTag("QL");
    pEmperor1->emperorInfo();
    Emperor *pEmperor2 = Emperor::getInstance();
    pEmperor2->emperorInfo();
    Emperor *pEmperor3 = Emperor::getInstance();
    pEmperor3->emperorInfo();
    Emperor::releaseInstance();
    getchar();
    return 0;
}

代码结构图:
这里写图片描述
运行结果:
这里写图片描述
单例模式比较简单,但在项目中使用的时候,需要明确只调用CEmperor的GetInstance函数来获取实例

4.(Multition多例模式)
和单例基本一样,是有个数限制的单例。如果对于产生的实例个数没有限制,那就不是多例了,和普通的类没有区别

#pragma once
#include <iostream>
#include <vector>
using namespace std;
class Emperor
{
private:
    string info;
    static int maxNumOfEmperor;
    static vector<Emperor*> emperor_list;
    Emperor();
    Emperor(string info);
    ~Emperor();
public:
    static Emperor* getInstance(int idx);
    void emperorInfo();
    static void releaseInstance();

};
#include "stdafx.h"
#include "Emperor.h"
#include <iostream>

int Emperor::maxNumOfEmperor = 3;
vector<Emperor*> Emperor::emperor_list;
Emperor::Emperor(void)
{
    cout << "create emperor instance" << endl;
}
Emperor::Emperor(string info)
{
    cout << "create emperor instance with info " << endl;
    this->info = info;
}
Emperor::~Emperor(void)
{
    cout << "destroy emperor instance and release its resources" << endl;
}
void Emperor::emperorInfo()
{
    char msg_buf[50] = {
  0};
    sprintf_s(msg_buf, 50, "the emperor's name is (%s)", this->info.c_str());
    string msg(msg_buf);
    cout << msg.c_str() << endl;
}
Emperor *Emperor::getInstance(int idx)
{
    if(emperor_list.empty())
    {
        for(int i = 0; i < maxNumOfEmperor; ++ i)
        {
            char name[10] = {
  0};
            sprintf_s(name, 10, "emperor %d", i);
            string tmp(name);
            Emperor *emp = new Emperor(tmp);
            emperor_list.push_back(emp);
        }
    }
    if(idx > -1 && idx < maxNumOfEmperor)
        return emperor_list.at(idx);
    return NULL;
}
void Emperor::releaseInstance()
{
    emperor_list.clear();
}

测试使用:

// MultitonPatternDemo.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Emperor.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Emperor *emperor1 = Emperor::getInstance(0);
    emperor1->emperorInfo();

    Emperor *emperor2 = Emperor::getInstance(1);
    emperor2->emperorInfo();

    Emperor *emperor3 = Emperor::getInstance(2);
    emperor3->emperorInfo();
    system("pause");
    return 0;
}

代码结构图:
这里写图片描述
运行结果:
这里写图片描述

5.(Factory Method工厂方法模式)
工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。
抽象基类IHuman

//IHuman.h
#pragma once
class IHuman
{
public:
    IHuman(void)
    {
    }
    virtual ~IHuman(void)
    {
    }
    virtual void Laugh() = 0;
    virtual void Cry() = 0;
    virtual void Talk() = 0;
};

实例类YellowHuman,继承自IHuman

//YellowHuman.h

#pragma once
#include "ihuman.h"
class CYellowHuman :
    public IHuman
{
public:
    CYellowHuman(void);
    ~CYellowHuman(void);
    void Laugh();
    void Cry();
    void Talk();
};
//YellowHuman.cpp

#include "StdAfx.h"
#include "YellowHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CYellowHuman::CYellowHuman(void)
{
}
CYellowHuman::~CYellowHuman(void)
{
}
void CYellowHuman::Cry()
{
    cout << "黄色人种会哭" << endl;
}
void CYellowHuman::Laugh()
{
    cout << "黄色人种会大笑,幸福呀!" << endl;
}
void CYellowHuman::Talk()
{
    cout << "黄色人种会说话,一般说的都是双字节" << endl;
}

实例类WhiteHuman,继承自IHuman

//WhiteHuman.h

#pragma once
#include "ihuman.h"
class CWhiteHuman :
    public IHuman
{
public:
    CWhiteHuman(void);
    ~CWhiteHuman(void);
    void Laugh();
    void Cry();
    void Talk();
};
//WhiteHuman.cpp

#include "StdAfx.h"
#include "WhiteHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CWhiteHuman::CWhiteHuman(void)
{
}
CWhiteHuman::~CWhiteHuman(void)
{
}
void CWhiteHuman::Cry()
{
    cout << "白色人种会哭" << endl;
}
void CWhiteHuman::Laugh()
{
    cout << "白色人种会大笑,侵略的笑声" << endl;
}
void CWhiteHuman::Talk()
{
    cout << "白色人种会说话,一般都是单字节" << endl;
}

实例类BlackHuman,继承自IHuman

//BlackHuman.h

#pragma once
#include "ihuman.h"
class CBlackHuman :
    public IHuman
{
public:
    CBlackHuman(void);
    ~CBlackHuman(void);
    void Laugh();
    void Cry();
    void Talk();
};
//BlackHuman.cpp

#include "StdAfx.h"
#include "BlackHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CBlackHuman::CBlackHuman(void)
{
}
CBlackHuman::~CBlackHuman(void)
{
}
void CBlackHuman::Cry()
{
    cout << "黑人会哭" << endl;
}
void CBlackHuman::Laugh()
{
    cout << "黑人会笑" << endl;
}
void CBlackHuman::Talk()
{
    cout << "黑人可以说话,一般人听不懂" << endl;
}

简单工厂实现

//SimpleHumanFactory.h
#pragma once
#include "IHuman.h"
#include <iostream>
using std::string;
class CSimpleHumanFactory
{
public:
    CSimpleHumanFactory(void);
    virtual ~CSimpleHumanFactory(void);
    virtual IHuman * CreateHuman(string classType);
};
// SimpleHumanFactory.cpp
#include "StdAfx.h"
#include "SimpleHumanFactory.h"
#include "YellowHuman.h"
#include "WhiteHuman.h"
#include "BlackHuman.h"
#include <iostream>
using std::string;


CSimpleHumanFactory::CSimpleHumanFactory(void)
{
}


CSimpleHumanFactory::~CSimpleHumanFactory(void)
{
}


IHuman * CSimpleHumanFactory::CreateHuman( string classType )
{
    if (classType.compare("CYellowHuman") == 0)
    {
        return new CYellowHuman();
    }
    else if(classType.compare("CWhiteHuman") == 0)
    {
        return new CWhiteHuman();
    }
    else if(classType.compare("CBlackHuman") == 0)
    {
        return new CBlackHuman();
    }
}

使用测试

// FactoryMethod.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "IHuman.h"
#include "YellowHuman.h"
#include "WhiteHuman.h"
#include "BlackHuman.h"
#include "SimpleHumanFactory.h"
#include "StandardHumanFactory.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;


void DoSimpleFactory()
{
    CSimpleHumanFactory *pSimpleHumanFactory = new CSimpleHumanFactory();
    cout << "----------第一批人是这样的:黄种人" << endl;
    IHuman *pYellowHuman = pSimpleHumanFactory->CreateHuman("CYellowHuman");
    pYellowHuman->Cry();
    pYellowHuman->Laugh();
    pYellowHuman->Talk();
    delete pYellowHuman;
    cout << "----------第二批人是这样的:白种人" << endl;
    IHuman *pWhiteHuman = pSimpleHumanFactory->CreateHuman("CWhiteHuman");
    pWhiteHuman->Cry();
    pWhiteHuman->Laugh();
    pWhiteHuman->Talk();
    delete pWhiteHuman;
    cout << "----------第三批人是这样的:黑种人" << endl;
    IHuman *pBlackHuman = pSimpleHumanFactory->CreateHuman("CBlackHuman");
    pBlackHuman->Cry();
    pBlackHuman->Laugh();
    pBlackHuman->Talk();
    delete pBlackHuman;
    cout << "----------第四批人是这样的:生产黄种人的工厂,采用了模板的方式。" << endl;
    CStandardHumanFactory<CYellowHuman> standardHumanFactory;
    pYellowHuman = standardHumanFactory.CreateHuman();
    pYellowHuman->Cry();
    pYellowHuman->Laugh();
    pYellowHuman->Talk();
    delete pYellowHuman;
}


int _tmain(int argc, _TCHAR* argv[])
{
    DoSimpleFactory();

    system("pause");
    return 0;
}

代码结构图:
这里写图片描述
运行结果:
这里写图片描述

6.(AbstractFactory抽象工厂模式)
抽象工厂,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。对于工厂方法来说,抽象工厂可实现一系列产品的生产,抽象工厂更注重产品的组合。
产品接口IHuman

//IHuman.h
#pragma once
class IHuman
{
public:

    IHuman(void)
    {
    }

    virtual ~IHuman(void)
    {
    }

    virtual void Laugh() = 0;
    virtual void Cry() = 0;
    virtual void Talk() = 0;
    virtual void Sex() = 0;
};

抽象产品之一YellowHuman

//YellowHuman.h

#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowHuman :
    public IHuman
{
public:
    CYellowHuman(void)
    {
    }
    ~CYellowHuman(void)
    {
    }
    void Laugh()
    {
        cout << "黄色人种会大笑,幸福呀!" << endl;
    }
    void Cry()
    {
        cout << "黄色人种会哭" << endl;
    }
    void Talk()
    {
        cout << "黄色人种会说话,一般说的都是双字节" << endl;
    }
    virtual void Sex() = 0;
};

具体产品之一,继承自YellowHuman

//YellowFemaleHuman.h

#pragma once
#include "yellowhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowFemaleHuman :
    public CYellowHuman
{
public:
    CYellowFemaleHuman(void)
    {
    }
    ~CYellowFemaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该黄种人的性别为女..." << endl;
    }
};

具体产品,继承自YellowHuman

//YellowMaleHuman.h

#pragma once
#include "yellowhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowMaleHuman :
    public CYellowHuman
{
public:
    CYellowMaleHuman(void)
    {
    }
    ~CYellowMaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该黄种人的性别为男..." << endl;
    }
};

抽象产品之二WhiteHuman

//WhiteHuman.h

#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteHuman :
    public IHuman
{
public:
    CWhiteHuman(void)
    {
    }
    ~CWhiteHuman(void)
    {
    }
    void Laugh()
    {
        cout << "白色人种会大笑,侵略的笑声" << endl;
    }
    void Cry()
    {
        cout << "白色人种会哭" << endl;
    }
    void Talk()
    {
        cout << "白色人种会说话,一般都是单字节" << endl;
    }
    virtual void Sex() = 0;
};

具体产品,继承自CWhiteHuman

//WhiteFemaleHuman.h

#pragma once
#include "whitehuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteFemaleHuman :
    public CWhiteHuman
{
public:
    CWhiteFemaleHuman(void)
    {
    }
    ~CWhiteFemaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该白种人的性别为女..." << endl;
    }
};

具体产品,继承自CWhiteHuman

//WhiteMaleHuman.h

#pragma once
#include "whitehuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteMaleHuman :
    public CWhiteHuman
{
public:
    CWhiteMaleHuman(void)
    {
    }
    ~CWhiteMaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该白种人的性别为男..." << endl;
    }
};

抽象产品之三

//BlackHuman.h

#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackHuman :
    public IHuman
{
public:
    CBlackHuman(void)
    {
    }
    ~CBlackHuman(void)
    {
    }
    void Laugh()
    {
        cout << "黑人会笑" << endl;
    }
    void Cry()
    {
        cout << "黑人会哭" << endl;
    }
    void Talk()
    {
        cout << "黑人可以说话,一般人听不懂" << endl;
    }

    virtual void Sex() = 0;
};

具体产品,继承自CBlackHuman

//BlackFemaleHuman.h

#pragma once
#include "blackhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackFemaleHuman :
    public CBlackHuman
{
public:
    CBlackFemaleHuman(void)
    {
    }
    ~CBlackFemaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该黑种人的性别为女..." << endl;
    }
};

具体产品,继承自CBlackHuman

//BlackMaleHuman.h

#pragma once
#include "blackhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackMaleHuman :
    public CBlackHuman
{
public:
    CBlackMaleHuman(void)
    {
    }
    ~CBlackMaleHuman(void)
    {
    }
    void Sex()
    {
        cout << "该黑种人的性别为男..." << endl;
    }
};

抽象工厂

//IHumanFactory.h

#pragma once
#include "IHuman.h"
class IHumanFactory
{
public:
    IHumanFactory(void)
    {
    }
    virtual ~IHumanFactory(void)
    {
    }
    virtual IHuman * CreateYellowHuman() = 0;
    virtual IHuman * CreateWhiteHuman() = 0;
    virtual IHuman * CreateBlackHuman() = 0;
};

抽象工厂基类(此类可有可无)

//StandardHumanFactory.h

#pragma once
#include "ihumanfactory.h"
#include "IHuman.h"
template<class T>
class CStandardHumanFactory :
    public IHumanFactory
{
public:
    CStandardHumanFactory(void)
    {
    }
    ~CStandardHumanFactory(void)
    {
    }
    IHuman * CreateHuman()
    {
        return new T;
    }
};

男人工厂

//MaleHumanFactory.h

#pragma once
#include "standardhumanfactory.h"
#include "IHumanFactory.h"
template<class T>
class CMaleHumanFactory :
    public CStandardHumanFactory<T>
{
public:
    CMaleHumanFactory(void);
    ~CMaleHumanFactory(void);
    IHuman * CreateYellowHuman();
    IHuman * CreateWhiteHuman();
    IHuman * CreateBlackHuman();
};
//MaleHumanFactory.cpp

#include "StdAfx.h"
#include "MaleHumanFactory.h"
template<class T>
CMaleHumanFactory<T>::CMaleHumanFactory(void)
{
}
template<class T>
CMaleHumanFactory<T>::~CMaleHumanFactory(void)
{
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateYellowHuman()
{
    return CreateHuman();
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateWhiteHuman()
{
    return CreateHuman();
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateBlackHuman()
{
    return CreateHuman();
}

女人工厂

//FemaleHumanFactory.h

#
  • 17
    点赞
  • 112
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值