外观模式(大话设计模式)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;
}

总结

相关推荐
white-persist18 分钟前
【攻防世界】reverse | IgniteMe 详细题解 WP
c语言·汇编·数据结构·c++·python·算法·网络安全
Bona Sun2 小时前
单片机手搓掌上游戏机(十六)—pico运行fc模拟器之程序修改烧录
c语言·c++·单片机·游戏机
小邓   ༽3 小时前
50道C++编程练习题及解答-C编程例题
c语言·汇编·c++·编程练习·c语言练习题
Bona Sun4 小时前
单片机手搓掌上游戏机(十三)—pico运行fc模拟器之硬件准备
c语言·c++·单片机·游戏机
Bona Sun4 小时前
单片机手搓掌上游戏机(十八)—pico运行fc模拟器之更大屏幕
c语言·c++·单片机·游戏机
白衣鸽子5 小时前
【基础数据篇】数据格式化妆师:Formatter模式
后端·设计模式
Yue丶越5 小时前
【C语言】自定义类型:结构体
c语言·开发语言
ZHE|张恒6 小时前
设计模式(十八)命令模式 —— 将操作封装成对象,实现撤销、队列等扩展
设计模式·命令模式
再睡一夏就好6 小时前
string.h头文件中strcpy、memset等常见函数的使用介绍与模拟实现
c语言·c++·笔记·string·内存函数·strcpy
dangdang___go6 小时前
动态内存管理||malloc和free.realloc和calloc
c语言·开发语言·算法·动态内存管理