外观模式

一、问题描述

客户购买和赎回基金,基金再购买和卖出多种股票或债券。过程相当于客户购买和赎回多种股票和债券,但客户并没有直接接触股票或债券。使用外观模式编写程序实现上述内容。

二、完成如下题目要求

(1)画出静态图

(2)写出采用该设计模式的好处

1.实现了子系统与客户端之间的松耦合关系。客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易。

(3)编写代码

#include<bits/stdc++.h>using namespace std;//股票1类class Stock1{ public: void Sell() { cout<<“股票1卖出”<<endl; } void Buy() { cout<<“股票1买入”<<endl; }};//股票2类class Stock2{ public: void Sell() { cout<<“股票2卖出”<<endl; } void Buy() { cout<<“股票2买入”<<endl; }};//国债1类class NationalDebt1{ public: void Sell() { cout<<“国债1卖出”<<endl; } void Buy() { cout<<“国债1买入”<<endl; }};//国债2类class NationalDebt2{ public: void Sell() { cout<<“国债2卖出”<<endl; } void Buy() { cout<<“国债2买入”<<endl; }};//基金类class Fund{ Stock1* stock1; Stock2* stock2; NationalDebt1* nationaldebt1; NationalDebt2* nationaldebt2; public: Fund() { stock1 = new Stock1(); stock2 = new Stock2(); nationaldebt1 = new NationalDebt1(); nationaldebt2 = new NationalDebt2(); } void BuyFund() { stock1->Buy(); stock2->Buy(); nationaldebt1->Buy(); nationaldebt2->Buy(); } void SellFund() { stock1->Sell(); stock2->Sell(); nationaldebt1->Sell(); nationaldebt2->Sell(); }};//Client类int main(){ Fund* fund = new Fund(); fund->BuyFund(); fund->SellFund(); return 0;}

学习会使你永远立于不败之地。

外观模式

相关文章:

你感兴趣的文章:

标签云: