设计模式 之美 -- 工厂方法模式
生活随笔
收集整理的這篇文章主要介紹了
设计模式 之美 -- 工厂方法模式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1. 解決問(wèn)題
- 2. 應(yīng)用場(chǎng)景
- 3. 實(shí)現(xiàn)如下
- C++實(shí)現(xiàn)
- C語(yǔ)言實(shí)現(xiàn)
- 4. 缺點(diǎn)
1. 解決問(wèn)題
在簡(jiǎn)單工廠模式中,我們使用賣衣服進(jìn)行舉例,同一種工廠可以賣很多不同種類的衣服,工廠只是將衣服的生產(chǎn)過(guò)程進(jìn)行了封裝。
當(dāng)我們?cè)黾右路N類的時(shí)候,在簡(jiǎn)單工廠模式中需要修改工廠的代碼,破壞了類的開閉原則(對(duì)擴(kuò)展開發(fā), 對(duì)修改關(guān)閉),同時(shí)增加測(cè)試成本。
此時(shí)為了避免這種問(wèn)題的出現(xiàn),我們推出工廠方法模式,為每個(gè)商品封裝指定的工廠方法,這樣商品種類增加時(shí)對(duì)于工廠只需要增加指定的工廠就可以了。
2. 應(yīng)用場(chǎng)景
- 邏輯中會(huì)經(jīng)常使用到該類對(duì)象(需要經(jīng)常進(jìn)行new、malloc操作)
- 邏輯后續(xù)會(huì)有一定規(guī)模,最好這個(gè)時(shí)候就將對(duì)象的創(chuàng)建和使用分離
- 后續(xù)會(huì)小范圍擴(kuò)展產(chǎn)品類型
- 程序規(guī)模比較大,不希望每次增加產(chǎn)品后都對(duì)之前邏輯重新測(cè)試
3. 實(shí)現(xiàn)如下
C++實(shí)現(xiàn)
實(shí)現(xiàn)功能:還是賣衣服,針對(duì)每一種衣服創(chuàng)建指定的工廠,由工廠封裝對(duì)象的創(chuàng)建過(guò)程。
#include <iostream>using namespace std;/*工廠的可以制造的衣服種類*/
enum ClothType{hat,paths};class Cloth{public:virtual void createCloth(void) = 0;virtual ~Cloth(){} //定義為虛函數(shù),對(duì)象空間回收時(shí)則會(huì)調(diào)用子類的析構(gòu)函數(shù)
};/*帽子類*/
class Hat: public Cloth{public:Hat(){cout << "Hat::hat()" << endl;}virtual void createCloth(void) {cout << "Hat::createHat()" << endl;}~Hat(){cout << "Hat::delete()" << endl;}
};/*褲子類*/
class Paths: public Cloth{public:Paths(){cout << "Paths::paths()" << endl;}virtual void createCloth(void) {cout << "Paths::createPaths()" << endl;}~Paths(){cout << "Paths::delete()" << endl;}
};/*工廠方法類,用來(lái)創(chuàng)建工廠*/
class Factory{
public:virtual Cloth *CreateProduct() = 0;
};/*帽子工廠,創(chuàng)建帽子*/
class FactoryHat : public Factory
{
public:Cloth *CreateProduct(){return new Hat ();}
};/*褲子工廠,創(chuàng)建褲子*/
class FactoryPaths : public Factory
{
public:Cloth *CreateProduct(){return new Paths ();}
};int main()
{Factory *factoryHat = new FactoryHat();Cloth *hat = factoryHat -> CreateProduct();hat -> createCloth();Factory *factoryPaths = new FactoryPaths();Cloth *paths = factoryPaths -> CreateProduct();paths -> createCloth();if(NULL != factoryHat) {delete factoryHat;factoryHat = NULL;}if(NULL != factoryPaths) {delete factoryPaths;factoryPaths = NULL;}if(NULL != hat) {delete hat;hat = NULL;}if(NULL != paths) {delete paths;paths = NULL;}return 0;
}
C語(yǔ)言實(shí)現(xiàn)
實(shí)現(xiàn)功能:生產(chǎn)橘子和蘋果兩種水果
/*用C語(yǔ)言實(shí)現(xiàn)工廠方法模式*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>enum {GRAPE,APPLE};typedef struct _Apple
{void (*print_apple)();
}Apple;typedef struct _Grape
{void (*print_grape)();
}Grape;void print_apple()
{printf("apple!\n");
}void print_grape()
{printf("grape!\n");
}Apple* sell_apple()
{Apple* pApple = (Apple*)malloc(sizeof(Apple));assert(NULL != pApple);pApple->print_apple = print_apple;return pApple;
}Grape* sell_grape()
{Grape* pGrape = (Grape*)malloc(sizeof(Grape));assert(NULL != pGrape);pGrape->print_grape = print_grape;return pGrape;
}typedef struct _FruitShop
{void* (*sell_fruit)();
}FruitShop;FruitShop* create_fruit_shop(int fruit)
{FruitShop* pFruitShop = (FruitShop*)malloc(sizeof(FruitShop));assert(NULL != pFruitShop);if (GRAPE == fruit){pFruitShop->sell_fruit = (void *(*)())sell_apple;}else if(APPLE == fruit){pFruitShop->sell_fruit = (void *(*)())sell_grape;}else{return NULL;}return pFruitShop;
}int main()
{FruitShop* apple = NULL;Apple* a = NULL;apple = create_fruit_shop(APPLE);a = (Apple *)apple->sell_fruit();a->print_apple();FruitShop* grape = NULL;Grape* g = NULL;grape = create_fruit_shop(GRAPE);g = (Grape*)grape->sell_fruit();g->print_grape();if(g != NULL){delete g;}if(grape != NULL){delete grape;}if(a != NULL){delete a;}if(apple != NULL){delete apple;}return 0;
}
4. 缺點(diǎn)
1、后續(xù)產(chǎn)品種類特別多的時(shí)候,需要建立很多工廠,導(dǎo)致程序熵值太大
2、對(duì)于有多個(gè)依賴關(guān)系的種類,如果仍然建立多個(gè)工廠,則會(huì)增加很多重復(fù)邏輯
總結(jié)
以上是生活随笔為你收集整理的设计模式 之美 -- 工厂方法模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 金桂多少钱啊?
- 下一篇: 编程模式 之美 -- 抽象工厂模式