您的位置:首页 > 其它

设计模式---外观模式 实例

2015-05-28 12:55 337 查看
代码:

#include <iostream>

using namespace std;

class stock

{

public:

virtual void sell() = 0;

virtual void buy() = 0;

};

class stock1 : public stock

{

public:

void sell()

{

cout<<"stock1 sell"<<"\n";

}

void buy()

{

cout<<"stock1 buy"<<"\n";

}

};

class stock2 : public stock

{

public:

void sell()

{

cout<<"stock2 sell"<<"\n";

}

void buy()

{

cout<<"stock2 buy"<<"\n";

}

};

class fund

{

private:

stock1 gp1;

stock2 gp2;

public:

void sellFund()

{

gp1.sell();

gp2.sell();

}

void buyFund()

{

gp1.buy();

gp2.buy();

}

};

int main()

{

fund jijin;

jijin.buyFund();

jijin.sellFund();

return 0;

}

运行结果:

ngnsvr9 [** NONE **]/home/xionghailong/demo/facade $ g++ main.cpp -o main

ngnsvr9 [** NONE **]/home/xionghailong/demo/facade $ ./main

stock1 buy

stock2 buy

stock1 sell

stock2 sell
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: