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

总结

相关推荐
qystca3 小时前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法
哪 吒8 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式
Theodore_10228 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou8 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
转世成为计算机大神11 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
搬砖的小码农_Sky11 小时前
C语言:数组
c语言·数据结构
小乖兽技术12 小时前
23种设计模式速记法
设计模式
小白不太白95013 小时前
设计模式之 外观模式
microsoft·设计模式·外观模式
小白不太白95013 小时前
设计模式之 原型模式
设计模式·原型模式
澄澈i13 小时前
设计模式学习[8]---原型模式
学习·设计模式·原型模式