[2] AbstractFactory)

Source: Internet
Author: User
Note: main (), nvwa IHuman, product interface CYellowHuman, CYellowFemaleHuman, one of the abstract products, CYellowMaleHuman, product 2 CWhiteHuman, product 2 CWhiteFemaleHuman, product 3 CWhiteMaleHuman, product 4 CBlackHuman: Abstract Product

Note: main (), nvwa IHuman, product interface CYellowHuman, CYellowFemaleHuman, one of the abstract products, CYellowMaleHuman, product 2 CWhiteHuman, product 2 CWhiteFemaleHuman, product 3 CWhiteMaleHuman, product 4 CBlackHuman: Abstract Product

Note:

Main (), nvwa

IHuman, product Interface

CYellowHuman, one of the abstract Products

CYellowFemaleHuman, one of the specific products

CYellowMaleHuman, product 2

CWhiteHuman, abstract product 2

CWhiteFemaleHuman, product 3

CWhiteMaleHuman, product 4

CBlackHuman, abstract product 3

CBlackFemaleHuman, product 5

CBlackMaleHuman, product 6

IHumanFactory, Abstract Factory

CStandardHumanFactory: Abstract Factory base class (this class can be unavailable)

CFemaleHumanFactory, one of the factories

CMaleHumanFactory, factory 2

Project directory:


3. Code Analysis

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
 
  
Using std: cout; using std: endl; CWhiteHuman: CWhiteHuman (void) {} CWhiteHuman ::~ CWhiteHuman (void) {} void CWhiteHuman: Cry () {cout <"White Cry" <endl;} void CWhiteHuman: Laugh () {cout <"white smile" <endl;} void CWhiteHuman: Talk () {cout <"white man said" <endl ;}
 
(2) WhiteMaleHuman

// WhiteMaleHuman. h # ifndef _ AbstractFactory _ WhiteMaleHuman __# define _ AbstractFactory _ WhiteMaleHuman __# include
 
  
# Include "WhiteHuman. h" using std: cout; using std: endl; class CWhiteMaleHuman: public CWhiteHuman {public: CWhiteMaleHuman (void ){}~ CWhiteMaleHuman (void) {} void Sex () {cout <"I am a white man" <endl ;}}; # endif/* defined (_ AbstractFactory _ WhiteMaleHuman _) * // WhiteMaleHuman. cpp null
 
(3) WhiteFemaleHuman

// WhiteFemaleHuman. h # ifndef _ AbstractFactory _ WhiteFemaleHuman __# define _ AbstractFactory _ WhiteFemaleHuman __# include
 
  
# Include "WhiteHuman. h" using std: cout; using std: endl; class CWhiteFemaleHuman: public CWhiteHuman {public: CWhiteFemaleHuman (void ){}~ CWhiteFemaleHuman (void) {} void Sex () {cout <"I am a white female" <endl ;}}; # endif/* defined (_ AbstractFactory _ WhiteFemaleHuman _) * // WhiteFemaleHuman. cpp null
 
2) Man-Black

(1) BlackHuman

// BlackHuman. h # ifndef _ AbstractFactory _ BlackHuman __# define _ AbstractFactory _ BlackHuman __# include
 
  
# 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
  
   
Using std: cout; using std: endl; CBlackHuman: CBlackHuman (void) {} CBlackHuman ::~ CBlackHuman (void) {} void CBlackHuman: Laugh () {cout <"Black smile" <endl;} void CBlackHuman: Cry () {cout <"black man crying" <endl;} void CBlackHuman: Talk () {cout <"black man says" <endl ;}
  
 
(2) BlackMaleHuman
// BlackMaleHuman. h # include
 
  
# Include "BlackHuman. h" using std: cout; using std: endl; class CBlackMaleHuman: public CBlackHuman {public: CBlackMaleHuman (void ){}~ CBlackMaleHuman (void) {} void Sex () {cout <"I am a black man" <endl ;}}; # endif/* defined (_ AbstractFactory _ BlackMaleHuman _) * // BlackMaleHuman. cpp null
 
(3) BlackFemaleHuman

// BlackFemaleHuman. h # ifndef _ AbstractFactory _ BlackFemaleHuman __# define _ AbstractFactory _ BlackFemaleHuman __# include
 
  
# Include "BlackHuman. h" using std: cout; using std: endl; class CBlackFemaleHuman: public CBlackHuman {public: CBlackFemaleHuman (void ){}~ CBlackFemaleHuman (void) {} void Sex () {cout <"I am a black female" <endl ;}}; # endif/* defined (_ AbstractFactory _ BlackFemaleHuman _) * // BlackFemaleHuman. cpp null
 
3) Man-Yellow

(1) YellowHuman

// 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
 
  
Using std: cout; using std: endl; CYellowHuman: CYellowHuman (void) {} CYellowHuman ::~ CYellowHuman (void) {} void CYellowHuman: Cry () {cout <"yellow man Cry" <endl;} void CYellowHuman: Laugh () {cout <"Yellow smile" <endl;} void CYellowHuman: Talk () {cout <"Yellow said" <endl ;}
 
(2) YellowMaleHuman

// YellowMaleHuman. h # include "YellowHuman. h" # include
 
  
Using std: cout; using std: endl; class CYellowMaleHuman: public CYellowHuman {public: CYellowMaleHuman (void ){}~ CYellowMaleHuman (void) {} void Sex () {cout <"I Am a male yellow man" <endl ;}; // YellowMaleHuman. cpp null
 
(3) YellowFemaleHuman

// YellowFemaleHuman. h # ifndef _ AbstractFactory _ YellowFemaleHuman __# define _ AbstractFactory _ YellowFemaleHuman __# include "YellowHuman. h" # include
 
  
Using std: cout; using std: endl; class CYellowFemaleHuman: public CYellowHuman {public: CYellowFemaleHuman (void ){}~ CYellowFemaleHuman (void) {} void Sex () {cout <"I am a female porn" <endl ;}}; # endif/* defined (_ AbstractFactory _ YellowFemaleHuman _) * // YellowFemaleHuman. cpp null
 
4) IHuman. h

#ifndef AbstractFactory_IHuman_h#define AbstractFactory_IHuman_hclass IHuman{public:    IHuman(void)    {            }    virtual ~IHuman(void)    {            }    virtual void Laugh() = 0;    virtual void Cry()   = 0;    virtual void Talk()  = 0;    virtual void Sex()   = 0;};#endif
Factory

(1) IHumanFactory. h

#ifndef AbstractFactory_IHumanFactory_h#define AbstractFactory_IHumanFactory_h#include 
 
  #include "IHuman.h"class IHumanFactory{public:    IHumanFactory(void)    {            }    virtual ~IHumanFactory(void)    {            }    virtual IHuman* CreateYellowHuman() = 0;    virtual IHuman* CreateWhiteHuman() = 0;    virtual IHuman* CreateBlackHuman() = 0;};#endif
 
(2) CStandardHumanFactory

// CStandardHumanFactory. h # ifndef _ AbstractFactory _ CStandardHumanFactory __# define _ AbstractFactory _ CStandardHumanFactory __# include
 
  
# Include "IHuman. h" # include "IHumanFactory. h" template
  
   
Class CStandardHumanFactory: public IHumanFactory {public: CStandardHumanFactory (void ){}~ CStandardHumanFactory (void) {}ihuman * CreateHuman () {return new T ;};# endif/* defined (_ AbstractFactory _ CStandardHumanFactory _) * // CStandardHumanFactory. cpp null
  
 
(3) MaleHumanFactory. h

//MaleHumanFactory.h#ifndef __AbstractFactory__MaleHumanFactory__#define __AbstractFactory__MaleHumanFactory__#include 
 
  #include "CStandardHumanFactory.h"#include "IHumanFactory.h"template
  
   class CMaleHumanFactory : public CStandardHumanFactory
   
    {public:    CMaleHumanFactory(void);    ~CMaleHumanFactory(void);    IHuman* CreateYellowHuman();    IHuman* CreateWhiteHuman();    IHuman* CreateBlackHuman();};#endif /* defined(__AbstractFactory__MaleHumanFactory__) *///MaleHumanFactory.cpp#include "MaleHumanFactory.h"template
    
     CMaleHumanFactory
     
      ::CMaleHumanFactory(void){    }template
      
       CMaleHumanFactory
       
        ::~CMaleHumanFactory(void){ }template
        
         IHuman* CMaleHumanFactory
         
          ::CreateYellowHuman(){ return CStandardHumanFactory
          
           ::CreateHuman();}template
           
            IHuman* CMaleHumanFactory
            
             ::CreateWhiteHuman(){ return CStandardHumanFactory
             
              ::CreateHuman();}template
              
               IHuman* CMaleHumanFactory
               
                ::CreateBlackHuman(){ return CStandardHumanFactory
                
                 ::CreateHuman();}
                
               
              
             
            
           
          
         
        
       
      
     
    
   
  
 
(4) FemaleHumanFactory

// FemaleHumanFactory. h # ifndef _ AbstractFactory _ FemaleHumanFactory __# define _ AbstractFactory _ FemaleHumanFactory __# include
 
  
# Include "CStandardHumanFactory. h" # include "IHuman. h" template
  
   
Class CFemaleHumanFactory: public CStandardHumanFactory
   
    
{Public: CFemaleHumanFactory (void ){}~ CFemaleHumanFactory (void) {} IHuman * CreateYellowHuman () {return CStandardHumanFactory
    
     
: CreateHuman ();} IHuman * CreateWhiteHuman () {return CStandardHumanFactory
     
      
: CreateHuman ();} IHuman * CreateBlackHuman () {return CStandardHumanFactory
      
        : CreateHuman () ;};# endif/* defined (_ AbstractFactory _ FemaleHumanFactory _) * // FemaleHumanFactory. cpp is empty
      
     
    
   
  
 
Main. cpp

# Include
 
  
# Include
  
   
# 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 <" ---------- start to create the yellow race ---------- "<endl; cout <"****** start to create a yellow women *****" <endl; IHumanFactory * pFemaleHumanFactory = new CFemaleHumanFactory
   
    
(); IHuman * pYellowFemaleHuman = pFemaleHumanFactory-> CreateYellowHuman (); pYellowFemaleHuman-> Cry (); comment-> Laugh (); pYellowFemaleHuman-> Talk (); pYellowFemaleHuman-> Sex (); delete pYellowFemaleHuman; delete pFemaleHumanFactory; cout <"****** start to create a yellow man *****" <endl; IHumanFactory * pMaleHumanFactory = new CMaleHumanFactory
    
     
(); IHuman * pYellowMaleHuman = pMaleHumanFactory-> CreateYellowHuman (); pYellowMaleHuman-> Cry (); pYellowMaleHuman-> Laugh (); pYellowMaleHuman-> Talk (); pYellowMaleHuman-> Sex (); delete pYellowMaleHuman; delete pMaleHumanFactory; cout <"---------- start to create white ----------" <endl; cout <"****** start to create white men *****" <endl; IHumanFactory * pMaleHumanFactory1 = new CMaleHumanFactory
     
      
(); IHuman * pWhiteMaleHuman = pMaleHumanFactory1-> CreateWhiteHuman (); pWhiteMaleHuman-> Cry (); pWhiteMaleHuman-> Laugh (); pWhiteMaleHuman-> Talk (); pWhiteMaleHuman-> Sex (); delete pMaleHumanFactory1; delete pWhiteMaleHuman; cout <"****** start to create white women *****" <endl; IHumanFactory * pFemaleHumanFactory1 = new CFemaleHumanFactory
      
        (); IHuman * pWhiteFemaleHuman = bytes-> CreateWhiteHuman (); pWhiteFemaleHuman-> Cry (); pWhiteFemaleHuman-> Laugh (); pWhiteFemaleHuman-> Talk (); pWhiteFemaleHuman-> Sex (); delete pFemaleHumanFactory1; delete pWhiteFemaleHuman; cout <"---------- start to create black --------" <endl; cout <"***** start to create a black man *****" <endl; IHumanFactory * pMaleHumanFactory2 = new CMaleHumanFactory
       
         (); IHuman * pBlackMaleHuman = pMaleHumanFactory2-> CreateBlackHuman (); pBlackMaleHuman-> Cry (); pBlackMaleHuman-> Laugh (); pBlackMaleHuman-> Talk (); pBlackMaleHuman-> Sex (); delete pMaleHumanFactory2; delete pBlackMaleHuman; cout <"***** start to create a black female *****" <endl; IHumanFactory * pFemaleHumanFactory2 = new CFemaleHumanFactory
        
          (); 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 <"Hello, World! \ N "; return 0 ;}
        
       
      
     
    
   
  
 
4. Run




References: Workshop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.