C语言实现《大话设计模式》中的外观模式例程#ifndef __FACADE_H__#define __FACADE_H__#include "rtthread.h"//各种小类#define FacadeClassCreate(ClassName,Str) \ typedef struct _##ClassName ClassName; \ struct _##ClassName \ { \ void (*##ClassName##Delete)(void *p##ClassName); \ void (*Sell)(void); \ void (*Buy)(void); \ }; \ static void ClassName##Delete(void *p##ClassName) \ { \ rt_free(p##ClassName); \ } \ static void ClassName##Sell(void) \ { \ rt_kprintf(" %s卖出!\n", Str); \ } \ static void ClassName##Buy(void) \ { \ rt_kprintf(" %s买入!\n", Str); \ } \ ClassName *ClassName##Create(rt_size_t Size) \ { \ ClassName *p##ClassName = (ClassName *)rt_malloc(Size); \ p##ClassName->##ClassName##Delete = ClassName##Delete; \ p##ClassName->Sell = ClassName##Sell; \ p##ClassName->Buy = ClassName##Buy; \ return p##ClassName; \ }FacadeClassCreate(Stock1, "股票1");FacadeClassCreate(Stock2, "股票2");FacadeClassCreate(Stock3, "股票3");FacadeClassCreate(NationalDebt1, "国债1");FacadeClassCreate(Realty1, "户地产1");//基金类typedef struct _Fund Fund;struct _Fund{ Stock1 *gu1; Stock2 *gu2; Stock3 *gu3; NationalDebt1 *nd1; Realty1 *rt1; void (*SellFund)(void *pFund); void (*BuyFund)(void *pFund); void (*FundDelete)(void *pFund);};static void FundDelete(void *pFund){ Stock1Delete(((Fund *)pFund)->gu1); Stock2Delete(((Fund *)pFund)->gu2); Stock3Delete(((Fund *)pFund)->gu3); NationalDebt1Delete(((Fund *)pFund)->nd1); Realty1Delete(((Fund *)pFund)->rt1); rt_free(pFund);}static void BuyFund(void *pFund){ ((Fund *)pFund)->gu1->Buy(); ((Fund *)pFund)->gu2->Buy(); ((Fund *)pFund)->gu3->Buy(); ((Fund *)pFund)->nd1->Buy(); ((Fund *)pFund)->rt1->Buy();}static void SellFund(void *pFund){ ((Fund *)pFund)->gu1->Sell(); ((Fund *)pFund)->gu2->Sell(); ((Fund *)pFund)->gu3->Sell(); ((Fund *)pFund)->nd1->Sell(); ((Fund *)pFund)->rt1->Sell(); }Fund *FundCreate(rt_size_t Size){ Fund *pFund = (Fund *)rt_malloc(Size); pFund->FundDelete = FundDelete; pFund->BuyFund = BuyFund; pFund->SellFund = SellFund; pFund->gu1 = Stock1Create(sizeof(Stock1)); pFund->gu2 = Stock2Create(sizeof(Stock2)); pFund->gu3 = Stock3Create(sizeof(Stock3)); pFund->nd1 = NationalDebt1Create(sizeof(NationalDebt1)); pFund->rt1 = Realty1Create(sizeof(Realty1)); return pFund;}#endif#include "Facade.h"#include "finsh.h"//客户端void Facade(void){ Fund *jijin = FundCreate(sizeof(Fund)); jijin->BuyFund(jijin); jijin->SellFund(jijin); jijin->FundDelete(jijin);}FINSH_FUNCTION_EXPORT(Facade, Facade model);用一个统一的外观类,来访问一组子系统,降低子系统与客户端的偶合。