外观模式(大话设计模式)C/C++版本

外观模式

C++

cpp 复制代码
#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

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;
}

总结

相关推荐
xinghuitunan36 分钟前
蓝桥杯顺子日期(填空题)
c语言·蓝桥杯
Half-up39 分钟前
C语言心型代码解析
c语言·开发语言
金池尽干39 分钟前
设计模式之——观察者模式
观察者模式·设计模式
也无晴也无风雨1 小时前
代码中的设计模式-策略模式
设计模式·bash·策略模式
懒大王就是我1 小时前
C语言网络编程 -- TCP/iP协议
c语言·网络·tcp/ip
半盏茶香1 小时前
【C语言】分支和循环详解(下)猜数字游戏
c语言·开发语言·c++·算法·游戏
小堇不是码农1 小时前
在VScode中配置C_C++环境
c语言·c++·vscode
小肥象不是小飞象2 小时前
(六千字心得笔记)零基础C语言入门第八课——函数(上)
c语言·开发语言·笔记·1024程序员节
励志成为嵌入式工程师6 小时前
c语言简单编程练习9
c语言·开发语言·算法·vim
Peter_chq7 小时前
【操作系统】基于环形队列的生产消费模型
linux·c语言·开发语言·c++·后端