Home > Database > Mysql Tutorial > 【2】设计模式之抽象工厂模式(AbstractFactory)

【2】设计模式之抽象工厂模式(AbstractFactory)

WBOY
Release: 2016-06-07 15:01:01
Original
908 people have browsed it

注释: main(),女娲 IHuman,产品接口 CYellowHuman,抽象产品之一 CYellowFemaleHuman,具体产品之一 CYellowMaleHuman,具体产品之二 CWhiteHuman,抽象产品之二 CWhiteFemaleHuman,具体产品之三 CWhiteMaleHuman,具体产品之四 CBlackHuman,抽象产品之

注释:

    main(),女娲

    IHuman,产品接口

    CYellowHuman,抽象产品之一

    CYellowFemaleHuman,具体产品之一

    CYellowMaleHuman,具体产品之二

    CWhiteHuman,抽象产品之二

    CWhiteFemaleHuman,具体产品之三

    CWhiteMaleHuman,具体产品之四

    CBlackHuman,抽象产品之三

    CBlackFemaleHuman,具体产品之五

    CBlackMaleHuman,具体产品之六

    IHumanFactory,抽象工厂

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

    CFemaleHumanFactory,工厂之一

    CMaleHumanFactory,工厂之二

工程目录:

【2】设计模式之抽象工厂模式(AbstractFactory)

3、代码分析

1)Man-White

(1)WhiteHuman

//WhiteHuman.h
#ifndef __AbstractFactory__WhiteHuman__
#define __AbstractFactory__WhiteHuman__

#include "IHuman.h"

class CWhiteHuman : public IHuman
{
public:
    CWhiteHuman(void);
    ~CWhiteHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__WhiteHuman__) */

//WhiteHuman.cpp
#include "WhiteHuman.h"
#include <iostream>
using std::cout;
using std::endl;

CWhiteHuman::CWhiteHuman(void)
{
    
}
CWhiteHuman::~CWhiteHuman(void)
{
    
}
void CWhiteHuman::Cry()
{
    cout (2)WhiteMaleHuman
<p></p>
<pre class="brush:php;toolbar:false">//WhiteMaleHuman.h
#ifndef __AbstractFactory__WhiteMaleHuman__
#define __AbstractFactory__WhiteMaleHuman__

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

class CWhiteMaleHuman : public CWhiteHuman
{
public:
    CWhiteMaleHuman(void)
    {
        
    }
    ~CWhiteMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout (3)WhiteFemaleHuman
<p></p>
<pre class="brush:php;toolbar:false">//WhiteFemaleHuman.h
#ifndef __AbstractFactory__WhiteFemaleHuman__
#define __AbstractFactory__WhiteFemaleHuman__

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

class CWhiteFemaleHuman : public CWhiteHuman
{
public:
    CWhiteFemaleHuman(void)
    {
        
    }
    ~CWhiteFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout 2)Man-Black
<p>(1)BlackHuman</p>
<p></p>
<pre class="brush:php;toolbar:false">//BlackHuman.h
#ifndef __AbstractFactory__BlackHuman__
#define __AbstractFactory__BlackHuman__
#include <iostream>
#include "IHuman.h"

class CBlackHuman : public IHuman
{
public:
    CBlackHuman(void);
    ~CBlackHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__BlackHuman__) */

//BlackHuman.cpp
#include "BlackHuman.h"
#include <iostream>
using std::cout;
using std::endl;

CBlackHuman::CBlackHuman(void)
{
    
}
CBlackHuman::~CBlackHuman(void)
{
    
}
void CBlackHuman::Laugh()
{
    cout (2)BlackMaleHuman<pre class="brush:php;toolbar:false">//BlackMaleHuman.h
#include <iostream>
#include "BlackHuman.h"
using std::cout;
using std::endl;

class CBlackMaleHuman : public CBlackHuman
{
public:
    CBlackMaleHuman(void)
    {
        
    }
    ~CBlackMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout (3)BlackFemaleHuman
<p></p>
<pre class="brush:php;toolbar:false">//BlackFemaleHuman.h
#ifndef __AbstractFactory__BlackFemaleHuman__
#define __AbstractFactory__BlackFemaleHuman__

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

class CBlackFemaleHuman : public CBlackHuman
{
public:
    CBlackFemaleHuman(void)
    {
        
    }
    ~CBlackFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout 3)Man-Yellow
<p>(1)YellowHuman<br>
</p>
<pre class="brush:php;toolbar:false">//YellowHuman.h
#ifndef __AbstractFactory__YellowHuman__
#define __AbstractFactory__YellowHuman__

#include "IHuman.h"

class CYellowHuman : public IHuman
{
public:
    CYellowHuman(void);
    ~CYellowHuman(void);
    void Laugh();
    void Cry();
    void Talk();
    virtual void Sex() = 0;
};

#endif /* defined(__AbstractFactory__YellowHuman__) */

//YellowHuman.cpp
#include "YellowHuman.h"
#include <iostream>
using std::cout;
using std::endl;

CYellowHuman::CYellowHuman(void)
{
    
}
CYellowHuman::~CYellowHuman(void)
{
    
}
void CYellowHuman::Cry()
{
    cout (2)YellowMaleHuman
<p></p>
<pre class="brush:php;toolbar:false">//YellowMaleHuman.h
#include "YellowHuman.h"
#include <iostream>
using std::cout;
using std::endl;

class CYellowMaleHuman : public CYellowHuman
{
public:
    CYellowMaleHuman(void)
    {
        
    }
    ~CYellowMaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout (3)YellowFemaleHuman
<p></p>
<pre class="brush:php;toolbar:false">//YellowFemaleHuman.h
#ifndef __AbstractFactory__YellowFemaleHuman__
#define __AbstractFactory__YellowFemaleHuman__

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

class CYellowFemaleHuman : public CYellowHuman
{
public:
    CYellowFemaleHuman(void)
    {
        
    }
    ~CYellowFemaleHuman(void)
    {
        
    }
    void Sex()
    {
        cout 4)IHuman.h
<p></p>
<pre class="brush:php;toolbar:false">#ifndef AbstractFactory_IHuman_h
#define AbstractFactory_IHuman_h

class IHuman
{
public:
    IHuman(void)
    {
        
    }
    virtual ~IHuman(void)
    {
        
    }
    virtual void Laugh() = 0;
    virtual void Cry()   = 0;
    virtual void Talk()  = 0;
    virtual void Sex()   = 0;
};

#endif
Copy after login
Factory

(1)IHumanFactory.h

#ifndef AbstractFactory_IHumanFactory_h
#define AbstractFactory_IHumanFactory_h

#include <iostream>
#include "IHuman.h"
class IHumanFactory
{
public:
    IHumanFactory(void)
    {
        
    }
    virtual ~IHumanFactory(void)
    {
        
    }
    virtual IHuman* CreateYellowHuman() = 0;
    virtual IHuman* CreateWhiteHuman() = 0;
    virtual IHuman* CreateBlackHuman() = 0;
};
#endif</iostream>
Copy after login
(2)CStandardHumanFactory

//CStandardHumanFactory.h
#ifndef __AbstractFactory__CStandardHumanFactory__
#define __AbstractFactory__CStandardHumanFactory__

#include <iostream>
#include "IHuman.h"
#include "IHumanFactory.h"

template<class t>
class CStandardHumanFactory : public IHumanFactory
{
public:
    CStandardHumanFactory(void)
    {
        
    }
    ~CStandardHumanFactory(void)
    {
        
    }
    IHuman* CreateHuman()
    {
        return new T;
    }
};

#endif /* defined(__AbstractFactory__CStandardHumanFactory__) */

//CStandardHumanFactory.cpp
空</class></iostream>
Copy after login
(3)MaleHumanFactory.h

//MaleHumanFactory.h

#ifndef __AbstractFactory__MaleHumanFactory__
#define __AbstractFactory__MaleHumanFactory__

#include <iostream>
#include "CStandardHumanFactory.h"
#include "IHumanFactory.h"

template<class t>
class CMaleHumanFactory : public CStandardHumanFactory<t>
{
public:
    CMaleHumanFactory(void);
    ~CMaleHumanFactory(void);
    IHuman* CreateYellowHuman();
    IHuman* CreateWhiteHuman();
    IHuman* CreateBlackHuman();
};

#endif /* defined(__AbstractFactory__MaleHumanFactory__) */

//MaleHumanFactory.cpp
#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 CStandardHumanFactory<t>::CreateHuman();
}
template<class t>
IHuman* CMaleHumanFactory<t>::CreateWhiteHuman()
{
    return CStandardHumanFactory<t>::CreateHuman();
}
template<class t>
IHuman* CMaleHumanFactory<t>::CreateBlackHuman()
{
    return CStandardHumanFactory<t>::CreateHuman();
}</t></t></class></t></t></class></t></t></class></t></class></t></class></t></class></iostream>
Copy after login
(4)FemaleHumanFactory

//FemaleHumanFactory.h
#ifndef __AbstractFactory__FemaleHumanFactory__
#define __AbstractFactory__FemaleHumanFactory__

#include <iostream>
#include "CStandardHumanFactory.h"
#include "IHuman.h"

template<class t>
class CFemaleHumanFactory : public CStandardHumanFactory<t>
{
public:
    CFemaleHumanFactory(void)
    {
        
    }
    ~CFemaleHumanFactory(void)
    {
        
    }
    IHuman* CreateYellowHuman()
    {
        return CStandardHumanFactory<t>::CreateHuman();
    }
    IHuman* CreateWhiteHuman()
    {
        return CStandardHumanFactory<t>::CreateHuman();
    }
    IHuman* CreateBlackHuman()
    {
        return CStandardHumanFactory<t>::CreateHuman();
    }
    
};

#endif /* defined(__AbstractFactory__FemaleHumanFactory__) */

//FemaleHumanFactory.cpp
空</t></t></t></t></class></iostream>
Copy after login
Main.cpp

#include <iostream>
#include <stdio.h>
#include "IHuman.h"
#include "IHumanFactory.h"
#include "FemaleHumanFactory.h"
#include "MaleHumanFactory.h"
#include "MaleHumanFactory.cpp"
#include "YellowFemaleHuman.h"
#include "YellowMaleHuman.h"
#include "WhiteFemaleHuman.h"
#include "WhiteMaleHuman.h"
#include "BlackFemaleHuman.h"
#include "BlackMaleHuman.h"

void DoMakeHuman()
{
    cout ();
    IHuman* pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
    pYellowFemaleHuman->Cry();
    pYellowFemaleHuman->Laugh();
    pYellowFemaleHuman->Talk();
    pYellowFemaleHuman->Sex();
    delete pYellowFemaleHuman;
    delete pFemaleHumanFactory;
    cout ();
    IHuman* pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
    pYellowMaleHuman->Cry();
    pYellowMaleHuman->Laugh();
    pYellowMaleHuman->Talk();
    pYellowMaleHuman->Sex();
    delete pYellowMaleHuman;
    delete pMaleHumanFactory;
    
    cout ();
    IHuman* pWhiteMaleHuman = pMaleHumanFactory1->CreateWhiteHuman();
    pWhiteMaleHuman->Cry();
    pWhiteMaleHuman->Laugh();
    pWhiteMaleHuman->Talk();
    pWhiteMaleHuman->Sex();
    delete pMaleHumanFactory1;
    delete pWhiteMaleHuman;
    cout ();
    IHuman* pWhiteFemaleHuman = pFemaleHumanFactory1->CreateWhiteHuman();
    pWhiteFemaleHuman->Cry();
    pWhiteFemaleHuman->Laugh();
    pWhiteFemaleHuman->Talk();
    pWhiteFemaleHuman->Sex();
    delete pFemaleHumanFactory1;
    delete pWhiteFemaleHuman;
    cout ();
    IHuman* pBlackMaleHuman = pMaleHumanFactory2->CreateBlackHuman();
    pBlackMaleHuman->Cry();
    pBlackMaleHuman->Laugh();
    pBlackMaleHuman->Talk();
    pBlackMaleHuman->Sex();
    delete pMaleHumanFactory2;
    delete pBlackMaleHuman;
    cout ();
    IHuman* pBlackFemaleHuman = pFemaleHumanFactory2->CreateBlackHuman();
    pBlackFemaleHuman->Cry();
    pBlackFemaleHuman->Laugh();
    pBlackFemaleHuman->Talk();
    pBlackFemaleHuman->Sex();
    delete pFemaleHumanFactory2;
    delete pBlackFemaleHuman;
}

int main(int argc, const char * argv[]) {

    DoMakeHuman();
    std::cout 
<h2>4、运行截图</h2>
<p><img  src="pic/20151007/x254qwmpe5a.png" alt="【2】设计模式之抽象工厂模式(AbstractFactory)" ><br>
</p>
<p><br>
</p>
<br>
<span>参考资料:http://blog.csdn.net/rexuefengye/article/details/12704821,感谢</span>my183100521<span>博主的精彩博文。</span>


</stdio.h></iostream>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template