外观模式
C++
# include <iostream>
using namespace std; class stock1
{
public : void Sell ( ) { cout << "股票1卖出" << endl; } void Buy ( ) { cout << "股票1买入" << endl; }
} ; class stock2
{
public : void Sell ( ) { cout << "股票2卖出" << endl; } void Buy ( ) { cout << "股票2买入" << endl; }
} ; class stock3
{
public : void Sell ( ) { cout << "股票3卖出" << endl; } void Buy ( ) { cout << "股票3买入" << endl; }
} ; class NationalDebt1
{
public : void Sell ( ) { cout << "国债1卖出" << endl; } void Buy ( ) { cout << "国债1买入" << endl; }
} ; class Realty1
{
public : void Sell ( ) { cout << "房地产1卖出" << endl; } void Buy ( ) { cout << "房地产1买入" << endl; }
} ; class Fund
{
private : stock1* gupiao1; stock2* gupiao2; stock3* gupiao3; NationalDebt1* guozhai; Realty1* fangdichan; public : Fund ( ) { gupiao1 = new stock1 ( ) ; gupiao2 = new stock2 ( ) ; gupiao3 = new stock3 ( ) ; guozhai = new NationalDebt1 ( ) ; fangdichan = new Realty1 ( ) ; } void BuyFund ( ) { gupiao1-> Buy ( ) ; gupiao2-> Buy ( ) ; gupiao3-> Buy ( ) ; guozhai-> Buy ( ) ; fangdichan-> Buy ( ) ; } void SellFund ( ) { gupiao1-> Sell ( ) ; gupiao2-> Sell ( ) ; gupiao3-> Sell ( ) ; guozhai-> Sell ( ) ; fangdichan-> Sell ( ) ; }
} ; int main ( )
{ Fund * jijin = new Fund ( ) ; jijin-> BuyFund ( ) ; cout << endl << "Two years later......\n" << endl; jijin-> SellFund ( ) ; return 0 ;
}
C
# include <stdio.h>
# include <stdlib.h> typedef struct
{ void ( * Sell) ( ) ; void ( * Buy) ( ) ;
} Invest; typedef struct
{ Invest base;
} Stock;
void Stock1_Sell ( )
{ printf ( "股票1卖出\n" ) ;
} void Stock1_Buy ( )
{ printf ( "股票1买入\n" ) ;
} Stock * createStock1 ( )
{ Stock * s = ( Stock * ) malloc ( sizeof ( Stock) ) ; s-> base. Sell = Stock1_Sell; s-> base. Buy = Stock1_Buy; return s;
}
typedef struct
{ Invest base;
} Stock2; void Stock2_Sell ( )
{ printf ( "股票2卖出\n" ) ;
} void Stock2_Buy ( )
{ printf ( "股票2买入\n" ) ;
} Stock2 * createStock2 ( )
{ Stock2 * s = ( Stock2 * ) malloc ( sizeof ( Stock2) ) ; s-> base. Sell = Stock2_Sell; s-> base. Buy = Stock2_Buy; return s;
}
typedef struct
{ Invest base;
} Stock3; void Stock3_Sell ( )
{ printf ( "股票3卖出\n" ) ;
} void Stock3_Buy ( )
{ printf ( "股票3买入\n" ) ;
} Stock3 * createStock3 ( )
{ Stock3 * s = ( Stock3 * ) malloc ( sizeof ( Stock3) ) ; s-> base. Sell = Stock3_Sell; s-> base. Buy = Stock3_Buy; return s;
}
typedef struct
{ Invest base;
} NationalDebt1; void NationalDebt1_Sell ( )
{ printf ( "国债1卖出\n" ) ;
} void NationalDebt1_Buy ( )
{ printf ( "国债买入\n" ) ;
} NationalDebt1 * createNationalDebt1 ( )
{ NationalDebt1 * s = ( NationalDebt1 * ) malloc ( sizeof ( NationalDebt1) ) ; s-> base. Sell = NationalDebt1_Sell; s-> base. Buy = NationalDebt1_Buy; return s;
}
typedef struct
{ Invest base;
} Realty1; void Realty1_Sell ( )
{ printf ( "房地产卖出\n" ) ;
} void Realty1_Buy ( )
{ printf ( "房地产3买入\n" ) ;
} Realty1 * createRealty1 ( )
{ Realty1 * s = ( Realty1 * ) malloc ( sizeof ( Realty1) ) ; s-> base. Sell = Realty1_Sell; s-> base. Buy = Realty1_Buy; return s;
}
typedef struct
{ Stock * gupiao1; Stock2 * gupiao2; Stock3 * gupiao3; NationalDebt1 * guozhai; Realty1 * fangdichan;
} Fund; Fund * createFund ( )
{ Fund * fund = ( Fund * ) malloc ( sizeof ( Fund) ) ; fund-> gupiao1 = createStock1 ( ) ; fund-> gupiao2 = createStock2 ( ) ; fund-> gupiao3 = createStock3 ( ) ; fund-> guozhai = createNationalDebt1 ( ) ; fund-> fangdichan = createRealty1 ( ) ; return fund;
} void BuyFund ( Fund * fund)
{ fund-> gupiao1-> base. Buy ( ) ; fund-> gupiao2-> base. Buy ( ) ; fund-> gupiao3-> base. Buy ( ) ; fund-> guozhai-> base. Buy ( ) ; fund-> fangdichan-> base. Buy ( ) ;
} void SellFund ( Fund * fund)
{ fund-> gupiao1-> base. Sell ( ) ; fund-> gupiao2-> base. Sell ( ) ; fund-> gupiao3-> base. Sell ( ) ; fund-> guozhai-> base. Sell ( ) ; fund-> fangdichan-> base. Sell ( ) ;
} int main ( )
{ Fund * jijin = createFund ( ) ; BuyFund ( jijin) ; printf ( "\nTwo years later...\n\n" ) ; SellFund ( jijin) ; free ( jijin-> gupiao1) ; free ( jijin-> gupiao2) ; free ( jijin-> gupiao3) ; free ( jijin-> guozhai) ; free ( jijin-> fangdichan) ; free ( jijin) ; return 0 ;
}
总结