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

总结

相关推荐
Ning_.1 小时前
力扣第116题:填充每个节点的下一个右侧节点指针 - C语言解法
c语言·算法·leetcode
挥剑决浮云 -2 小时前
STM32学习之 按键/光敏电阻 控制 LED/蜂鸣器
c语言·经验分享·stm32·单片机·嵌入式硬件·学习
zfenggo3 小时前
c/c++ 无法跳转定义
c语言·开发语言·c++
图灵猿3 小时前
【Lua之·Lua与C/C++交互·Lua CAPI访问栈操作】
c语言·c++·lua
A懿轩A3 小时前
C/C++ 数据结构与算法【树和二叉树】 树和二叉树,二叉树先中后序遍历详细解析【日常学习,考研必备】带图+详细代码
c语言·数据结构·c++·学习·二叉树·
bandaoyu3 小时前
【设计模式】装饰器模式(Decorator Pattern)
设计模式·装饰器模式
lijiachang0307185 小时前
设计模式(一):单例模式
c++·笔记·学习·程序人生·单例模式·设计模式·大学生
抓哇FullStack-Junior5 小时前
设计模式——适配器模式
java·设计模式·适配器模式
silver6877 小时前
桥接模式详解
设计模式
思忖小下7 小时前
梳理你的思路(从OOP到架构设计)_设计模式Observer模式
观察者模式·设计模式·eit