1、外观模式
2、代码
#include <iostream>
#include <string.h>
using namespace std;
class Stock1
{
public:
void sell()
{
printf("sell stock1\n");
}
void buy()
{
printf("buy stock1\n");
}
};
class Stock2
{
public:
void sell()
{
printf("sell stock2\n");
}
void buy()
{
printf("buy stock2\n");
}
};
class Fund
{
public:
Fund()
{
st1 = new Stock1;
st2 = new Stock2;
}
void BuyFund()
{
st1->buy();
st2->buy();
}
void SellFund()
{
st1->sell();
st2->sell();
}
~Fund()
{
delete st1;
delete st2;
}
private:
Stock1* st1;
Stock2* st2;
};
void main()
{
Fund* fund = new Fund;
//购买基金
fund->BuyFund();
fund->SellFund();
delete fund;
return;
}
3、特点:
(1)对于基类基金来说,他了解所有的股票的信息,并为外部提供接口供调用。
(2)对于用户来说,他不需要了解股票,这些由基金负责。