C语言设计模式:工厂和抽象工厂

http://blog.csdn.net/feixiaoxing/article/details/7081243

http://blog.csdn.net/feixiaoxing/article/details/7089626


这哥们两个例子举得非常好。

工厂就是提取产品的一个维度,抽象工厂就是多个维度,甚至于把工厂也抽象了。

如果说工厂模式搞多个参数支持多个维度,也行,但代码就难看了。

从代码可以看出来,这两种模式的缺陷就是,需要事先知道产品的类型。

[cpp]  view plain copy
  1. typedef struct _Shoe  
  2. {  
  3.     int type;  
  4.     void (*print_shoe)(struct _Shoe*);  
  5. }Shoe;  
    就像上面说的,现在有胶鞋,那也有皮鞋,我们该怎么做呢?

[cpp]  view plain copy
  1. void print_leather_shoe(struct _Shoe* pShoe)  
  2. {  
  3.     assert(NULL != pShoe);  
  4.     printf("This is a leather show!\n");  
  5. }  
  6.   
  7. void print_rubber_shoe(struct _Shoe* pShoe)  
  8. {  
  9.     assert(NULL != pShoe);  
  10.     printf("This is a rubber shoe!\n");  
  11. }  
    所以,对于一个工厂来说,创建什么样的鞋子,就看我们输入的参数是什么?至于结果,那都是一样的。

[cpp]  view plain copy
  1. #define LEATHER_TYPE 0x01  
  2. #define RUBBER_TYPE  0x02  
  3.   
  4. Shoe* manufacture_new_shoe(int type)  
  5. {  
  6.     assert(LEATHER_TYPE == type || RUBBER_TYPE == type);  
  7.   
  8.     Shoe* pShoe = (Shoe*)malloc(sizeof(Shoe));  
  9.     assert(NULL != pShoe);  
  10.   
  11.     memset(pShoe, 0, sizeof(Shoe));  
  12.     if(LEATHER_TYPE == type)  
  13.     {  
  14.         pShoe->type == LEATHER_TYPE;  
  15.         pShoe->print_shoe = print_leather_shoe;  
  16.     }  
  17.     else  
  18.     {  
  19.         pShoe->type == RUBBER_TYPE;  
  20.         pShoe->print_shoe = print_rubber_shoe;  
  21.     }  
  22.   
  23.     return pShoe;  

[cpp]  view plain copy
  1. typedef struct _Apple  
  2. {  
  3.     void (*print_apple)();  
  4. }Apple;  
  5.   
  6. typedef struct _Grape  
  7. {  
  8.     void (*print_grape)();  
  9. }Grape;   
    上面分别对苹果和葡萄进行了抽象,当然它们的具体函数也是不一样的。

[cpp]  view plain copy
  1. void print_white_apple()  
  2. {  
  3.     printf("white apple!\n");  
  4. }  
  5.   
  6. void print_red_apple()  
  7. {  
  8.     printf("red apple!\n");  
  9. }  
  10.   
  11. void print_white_grape()  
  12. {  
  13.     printf("white grape!\n");  
  14. }  
  15.   
  16. void print_red_grape()  
  17. {  
  18.     printf("red grape!\n");  
  19. }  
    完成了水果函数的定义。下面就该定义工厂了,和水果一样,我们也需要对工厂进行抽象处理。

[cpp]  view plain copy
  1. typedef struct _FruitShop  
  2. {  
  3.     Apple* (*sell_apple)();  
  4.     Apple* (*sell_grape)();  
  5. }FruitShop;  
    所以,对于卖白苹果、白葡萄的水果店就该这样设计了,红苹果、红葡萄的水果店亦是如此。

[cpp]  view plain copy
  1. Apple* sell_white_apple()  
  2. {  
  3.     Apple* pApple = (Apple*) malloc(sizeof(Apple));  
  4.     assert(NULL != pApple);  
  5.   
  6.     pApple->print_apple = print_white_apple;  
  7.     return pApple;  
  8. }  
  9.   
  10. Grape* sell_white_grape()  
  11. {  
  12.     Grape* pGrape = (Grape*) malloc(sizeof(Grape));  
  13.     assert(NULL != pGrape);  
  14.   
  15.     pGrape->print_grape = print_white_grape;  
  16.     return pGrape;  
  17. }   
    这样,基本的框架就算搭建完成的,以后创建工厂的时候,

[cpp]  view plain copy
  1. FruitShop* create_fruit_shop(int color)  
  2. {  
  3.     FruitShop* pFruitShop = (FruitShop*) malloc(sizeof(FruitShop));  
  4.     assert(NULL != pFruitShop);  
  5.   
  6.     if(WHITE == color)  
  7.     {  
  8.         pFruitShop->sell_apple = sell_white_apple;  
  9.         pFruitShop->sell_grape = sell_white_grape;  
  10.     }  
  11.     else  
  12.     {  
  13.         pFruitShop->sell_apple = sell_red_apple;  
  14.         pFruitShop->sell_grape = sell_red_grape;  
  15.     }  
  16.   
  17.     return pFruitShop;  
  18. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值