日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > > 嵌入式大雜燴
[導(dǎo)讀]我們接簡單工廠方法模式_C語言實(shí)現(xiàn)中生產(chǎn)電腦的模擬場景來看。


簡介

我們接簡單工廠方法模式_C語言實(shí)現(xiàn)中生產(chǎn)電腦的模擬場景來看。

在簡單工廠方法中,不同供應(yīng)商的硬盤都由HardDiskFactory創(chuàng)建出來。這種做法存在幾個(gè)問題:

1、當(dāng)供應(yīng)商很多時(shí),HardDiskFactory就會(huì)成為一個(gè)“過大類”

2、假設(shè)現(xiàn)在要新引入一家硬盤供應(yīng)商,那就需要在HardDiskFactory的Create方法中增加一個(gè)case分支。在Create方法中增加case分支的做法違反了“開閉原則”。

我們現(xiàn)在要講的“工廠方法”可以解決上述問題:

工廠方法針對每一個(gè)供應(yīng)商的硬盤提供一個(gè)工廠。通過不同工廠對象來創(chuàng)建不同的產(chǎn)品對象。也就是說,工廠方法不直接定義用于創(chuàng)建對象的類,而是定義一個(gè)創(chuàng)建硬盤對象的接口,讓子類決定實(shí)例化哪一個(gè)供應(yīng)商的硬盤。

當(dāng)新增一個(gè)硬盤供應(yīng)商時(shí),只需要新增一個(gè)工廠對象。不需要在原有工廠中增加代碼,所以不違反“開閉原則”,也不會(huì)因?yàn)椴粩嘣黾庸?yīng)商而產(chǎn)生“過大類”。

硬盤對象創(chuàng)建

參與者

1、Product: HardDisk

定義硬盤對象的接口(與簡單工廠方法相同)

2、Concrete Product: SeagateHardDisk, ToshibaHardDisk

實(shí)現(xiàn)不同供應(yīng)商的硬盤(與簡單工廠方法相同)

3、Factory: HardDiskFactory

聲明硬盤工廠方法,具體硬盤對象的創(chuàng)建交給其子類 ConcreteFactory

4、ConcreteFactory: SeagateHardDiskFactory, ToshibaHardDiskFactory

創(chuàng)建具體硬盤對象

UML

HardDisk、SeagateHardDisk、ToshibaHardDisk示例代碼與簡單工廠方法完全一致,此處不再重復(fù)

HardDiskFactory代碼示例:

hard_disk_factory.h:

#ifndef HARD_DISK_FACTORY_H #define HARD_DISK_FACTORY_H #include "hard_disk.h" struct HardDiskFactory { struct HardDisk* (*Create)(struct HardDiskFactory *this); void (*Destroy)(struct HardDiskFactory *this, 
                    struct HardDisk* hardDisk);
}; #endif 

SeagateHardDiskFactory代碼示例:

seagate_hard_disk_factory.h:

#define SEAGATE_HARD_DISK_FACTORY_H #include "hard_disk_factory.h" struct SeagateHardDiskFactory { struct HardDiskFactory hardDiskFactory; }; // 構(gòu)造函數(shù) void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this); // 析構(gòu)函數(shù) void _SeagateHardDiskFactory(struct SeagateHardDiskFactory *this); #endif 

seagate_hard_disk_factory.c:

#include "seagate_hard_disk_factory.h" #include "seagate_hard_disk.h" #include "stdio.h" #include "stdlib.h" struct HardDisk* SeagateCreate(struct HardDiskFactory *this) { struct SeagateHardDisk *seagateHardDisk = NULL; if ((seagateHardDisk = malloc(sizeof(struct SeagateHardDisk))) == NULL) { printf("fail in malloc\n"); return NULL;
    }
    SeagateHardDisk(seagateHardDisk); return (struct HardDisk*)seagateHardDisk;
} void SeagateDestroy(struct HardDiskFactory *this, struct HardDisk *hardDisk) { if (hardDisk != NULL) { free(hardDisk);
        hardDisk = NULL;
    }    
} // 構(gòu)造函數(shù) void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this) { this->hardDiskFactory.Create = SeagateCreate; this->hardDiskFactory.Destroy = SeagateDestroy;
} // 析構(gòu)函數(shù) void _SeagateHardDiskFactory(struct SeagateHardDiskFactory *this)
{ this->hardDiskFactory.Create = NULL; this->hardDiskFactory.Destroy = NULL;
}

ToshibaHardDiskFactory代碼示例:

toshiba_hard_disk_factory.h:

#ifndef TOSHIBA_HARD_DISK_FACTORY_H #define TOSHIBA_HARD_DISK_FACTORY_H #include "hard_disk_factory.h" struct ToshibaHardDiskFactory { struct HardDiskFactory hardDiskFactory; }; // 構(gòu)造函數(shù) void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this); // 析構(gòu)函數(shù) void _ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this); #endif 

toshiba_hard_disk_factory.c:

#include "toshiba_hard_disk_factory.h" #include "toshiba_hard_disk.h" #include "stdio.h" #include "stdlib.h" struct HardDisk* ToshibaCreate(struct HardDiskFactory *this) { struct ToshibaHardDisk *toshibaHardDisk = NULL; if ((toshibaHardDisk = malloc(sizeof(struct ToshibaHardDisk))) == NULL) { printf("fail in malloc\n"); return NULL;
    }
    ToshibaHardDisk(toshibaHardDisk); return (struct HardDisk*)toshibaHardDisk;
} void ToshibaDestroy(struct HardDiskFactory *this, struct HardDisk *hardDisk) { if (hardDisk != NULL) { free(hardDisk);
        hardDisk = NULL;
    }    
} // 構(gòu)造函數(shù) void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this) { this->hardDiskFactory.Create = ToshibaCreate; this->hardDiskFactory.Destroy = ToshibaDestroy;
} // 析構(gòu)函數(shù) void _ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this)
{ this->hardDiskFactory.Create = NULL; this->hardDiskFactory.Destroy = NULL;
}

客戶端代碼示例:

#include "hard_disk.h" #include "hard_disk_factory.h" #include "seagate_hard_disk_factory.h" #include "toshiba_hard_disk_factory.h" #include "stddef.h" void main() { struct HardDisk *hardDisk = NULL; struct HardDiskFactory *hardDiskFactory; struct SeagateHardDiskFactory seagateHardDiskFactory; SeagateHardDiskFactory(&seagateHardDiskFactory);
    hardDiskFactory = (struct HardDiskFactory *)&seagateHardDiskFactory; // 創(chuàng)建 seagate 硬盤對象 hardDisk = hardDiskFactory->Create(hardDiskFactory); // 使用 seagate 硬盤對象 hardDisk->Operation(hardDisk); // 銷毀 seagate 硬盤對象 hardDiskFactory->Destroy(hardDiskFactory, hardDisk);
    _SeagateHardDiskFactory(&seagateHardDiskFactory); struct ToshibaHardDiskFactory toshibaHardDiskFactory; ToshibaHardDiskFactory(&toshibaHardDiskFactory);
    hardDiskFactory = (struct HardDiskFactory *)&toshibaHardDiskFactory; // 創(chuàng)建 toshiba 硬盤對象 hardDisk = hardDiskFactory->Create(hardDiskFactory); // 使用 seagate 硬盤對象 hardDisk->Operation(hardDisk); // 銷毀 toshiba 硬盤對象 hardDiskFactory->Destroy(hardDiskFactory, hardDisk);
    _ToshibaHardDiskFactory(&toshibaHardDiskFactory);
}

客戶端顯示示例:

./hard_disk
這是 Seagate 硬盤
這是 Toshiba 硬盤


免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場,如有問題,請聯(lián)系我們,謝謝!

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉