chinese直男口爆体育生外卖, 99久久er热在这里只有精品99, 又色又爽又黄18禁美女裸身无遮挡, gogogo高清免费观看日本电视,私密按摩师高清版在线,人妻视频毛茸茸,91论坛 兴趣闲谈,欧美 亚洲 精品 8区,国产精品久久久久精品免费

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

C語言簡單工廠方法實例

STM32嵌入式開發(fā) ? 來源:CSDN技術社區(qū) ? 2023-04-03 10:06 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

1 簡介

簡單工廠方法定義一個用于創(chuàng)建對象的類,該類接受一個參數(shù),通過參數(shù)決定創(chuàng)建不同的對象。

GOF并沒有把簡單工廠方法定義為23種設計模式之一,可以認為簡單工廠方法是工廠方法的簡化形式。

為了體現(xiàn)簡單工廠方法和工廠方法的區(qū)別和聯(lián)系,此處把簡單工廠方法先單獨講一下。

2 模擬場景

假設你要生產(chǎn)電腦,電腦由硬盤、內(nèi)存條、CPU、主板的部件組成。你為了保證供應鏈可靠,每種部件都選擇了至少兩家供應商。比如: 硬盤供應商 seagate、Toshiba 內(nèi)存條供應商 SAMSUNG、Crucial CPU供應商 intelAMD 主板供應商 intel、AMD 此處列出多個部件是為了后面講解工廠方法、抽象工廠方法時使用同一個模擬場景。本章講簡單工廠方法暫時不需要涉及這么多部件,所以僅以硬盤這一個部件為例進行講解。

3 實現(xiàn)的思路

硬盤就是要創(chuàng)建的對象(即:產(chǎn)品)。為了讓不同供應商提供的硬盤可以通用,要定義一個硬盤產(chǎn)品類,并讓不同供應商的硬盤都繼承硬盤產(chǎn)品類的接口

還需要定義一個創(chuàng)建硬盤對象的類(即:工廠)。工廠類根據(jù)參數(shù)決定創(chuàng)建哪家供應商的硬盤對象。

4 實現(xiàn)硬盤對象創(chuàng)建

參與者: (1)Product: HardDisk 定義硬盤對象的接口 (2)Concrete Product: SeagateHardDisk, ToshibaHardDisk 實現(xiàn)不同供應商的硬盤 (3)SimpleFactory: HardDiskFactory 根據(jù)參數(shù),創(chuàng)建不同供應商的硬盤對象

UML:

1005c45c-d136-11ed-bfe3-dac502259ad0.png

HardDisk代碼示例:

hard_disk.h:


#ifndef HARD_DISK_H
#define HARD_DISK_H


struct HardDisk {
    void (*Operation)(struct HardDisk *this);
};


#endif
SeagateHardDisk代碼示例:

seagate_hard_disk.h:

#ifndef SEAGATE_HARD_DISK_H
#define SEAGATE_HARD_DISK_H


#include "hard_disk.h"


struct SeagateHardDisk {
    struct HardDisk hardDisk;
};


// 構造函數(shù)
void SeagateHardDisk(struct SeagateHardDisk *this);


// 析構函數(shù)
void _SeagateHardDisk(struct SeagateHardDisk *this);


#endif
seagate_hard_disk.c:

#include "seagate_hard_disk.h"
#include "stdio.h"


void SeagateOperation(struct SeagateHardDisk *this)
{
    printf("這是 Seagate 硬盤
");
}


void SeagateHardDisk(struct SeagateHardDisk *this)
{
    this->hardDisk.Operation = (void(*)(struct HardDisk *))SeagateOperation;
}


void _SeagateHardDisk(struct SeagateHardDisk *this)
{
    this->hardDisk.Operation = NULL;
}
ToshibaHardDisk代碼示例:

toshiba_hard_disk.h:

#ifndef TOSHIBA_HARD_DISK_H
#define TOSHIBA_HARD_DISK_H


#include "hard_disk.h"


struct ToshibaHardDisk {
    struct HardDisk hardDisk;
};


// 構造函數(shù)
void ToshibaHardDisk(struct ToshibaHardDisk *this);


// 析構函數(shù)
void _ToshibaHardDisk(struct ToshibaHardDisk *this);


#endif
toshiba_hard_disk.c:

#include "toshiba_hard_disk.h"
#include "stdio.h"


void ToshibaOperation(struct ToshibaHardDisk *this)
{
    printf("這是 Toshiba 硬盤
");
}


void ToshibaHardDisk(struct ToshibaHardDisk *this)
{
    this->hardDisk.Operation = (void(*)(struct HardDisk *))ToshibaOperation;
}


void _ToshibaHardDisk(struct ToshibaHardDisk *this)
{
    this->hardDisk.Operation = NULL;
}
HardDiskFactory代碼示例:

hard_disk_factory.h:

#ifndef HARD_DISK_FACTORY_H
#define HARD_DISK_FACTORY_H


#include "hard_disk.h"


enum HARD_DISK_SUPPLIER_E {
    HARD_DISK_SUPPLIER_SEAGATE,
    HARD_DISK_SUPPLIER_TOSHIBA
};


struct HardDiskFactory {
    struct HardDisk* (*Create)(struct HardDiskFactory *this, 
                               enum HARD_DISK_SUPPLIER_E supplier);
    void (*Destroy)(struct HardDiskFactory *this, 
                    struct HardDisk* hardDisk);
};


// 構造函數(shù)
void HardDiskFactory(struct HardDiskFactory *this);


// 析構函數(shù)
void _HardDiskFactory(struct HardDiskFactory *this);


#endif
hard_disk_factory.c:

#include "hard_disk_factory.h"
#include "seagate_hard_disk.h"
#include "toshiba_hard_disk.h"
#include "stdio.h"
#include "stdlib.h"


struct HardDisk *Create(struct HardDiskFactory *this, 
                        enum HARD_DISK_SUPPLIER_E supplier) 
{
    switch (supplier) {
        case HARD_DISK_SUPPLIER_SEAGATE:
        {
            struct SeagateHardDisk *seagateHardDisk = NULL;
            if ((seagateHardDisk = malloc(sizeof(struct SeagateHardDisk))) == NULL) {
                printf("fail in malloc
");
                return NULL;
            }
            SeagateHardDisk(seagateHardDisk);
            return (struct HardDisk *)seagateHardDisk;
        }
        case HARD_DISK_SUPPLIER_TOSHIBA:
        {
            struct ToshibaHardDisk *toshibaHardDisk = NULL;
            if ((toshibaHardDisk = malloc(sizeof(struct ToshibaHardDisk))) == NULL) {
                printf("fail in malloc
");
                return NULL;
            }
            ToshibaHardDisk(toshibaHardDisk);
            return (struct HardDisk *)toshibaHardDisk;
        }
        default:
            printf("未知的供應商
");
            return NULL;
    }
}


void Destroy(struct HardDiskFactory *this, struct HardDisk* hardDisk)
{
    if (hardDisk != NULL) {
        free(hardDisk);
    }
}


// 構造函數(shù)
void HardDiskFactory(struct HardDiskFactory *this)
{
    this->Create = Create;
    this->Destroy = Destroy;
}


// 析構函數(shù)
void _HardDiskFactory(struct HardDiskFactory *this)
{
    this->Create = NULL;
    this->Destroy = NULL;
}
客戶端代碼示例:

#include "hard_disk.h"
#include "hard_disk_factory.h"
#include "stddef.h"


void main()
{
    struct HardDisk *hardDisk = NULL;


    struct HardDiskFactory hardDiskFactory;
    HardDiskFactory(&hardDiskFactory);
    
    // 創(chuàng)建 seagate 硬盤對象
    hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_SEAGATE);
    // 使用 seagate 硬盤對象
    hardDisk->Operation(hardDisk);  
    // 銷毀 seagate 硬盤對象
    hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);       
    
    // 創(chuàng)建 toshiba 硬盤對象
    hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_TOSHIBA);
    // 使用 seagate 硬盤對象
    hardDisk->Operation(hardDisk);
    // 銷毀 toshiba 硬盤對象
    hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);    
    
    _HardDiskFactory(&hardDiskFactory);
}
客戶端顯示示例:

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

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • amd
    amd
    +關注

    關注

    25

    文章

    5586

    瀏覽量

    136331
  • cpu
    cpu
    +關注

    關注

    68

    文章

    11077

    瀏覽量

    217025
  • 接口
    +關注

    關注

    33

    文章

    9000

    瀏覽量

    153712
  • 硬盤
    +關注

    關注

    3

    文章

    1338

    瀏覽量

    58441
  • C語言
    +關注

    關注

    180

    文章

    7632

    瀏覽量

    141670

原文標題:C語言簡單工廠方法實例

文章出處:【微信號:c-stm32,微信公眾號:STM32嵌入式開發(fā)】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

    相關推薦
    熱點推薦

    單片機C語言編程實例

    `本書結合目前應用非常廣泛的C語言以及Keil C51編譯器,詳細介紹單片機C語言編程的方法。本
    發(fā)表于 06-22 10:29

    單片機C語言實例有哪些

    單片機C語言實例(400例)/單片機C語言實例(400例)/1-IO輸出-點亮1個LED燈方法1/單片機
    發(fā)表于 07-15 10:06

    C語言與MATLAB接口編程與實例

    本書以簡潔的語言、豐富的實例系統(tǒng)地介紹了C語言與 MATLAB 接口函數(shù)(稱之為:C-MEX函數(shù))的編程
    發(fā)表于 08-08 11:23 ?0次下載
    <b class='flag-5'>C</b><b class='flag-5'>語言</b>與MATLAB接口編程與<b class='flag-5'>實例</b>

    C語言和匯編語言混合編程方法C語言中斷處理方法

    C語言和匯編語言混合編程方法C語言中斷處理方法,n
    發(fā)表于 01-06 14:36 ?36次下載

    單片機C語言編程與實例

    單片機C語言編程與實例 學習單片機開發(fā)非常不錯的資料。
    發(fā)表于 01-11 14:50 ?44次下載

    5402 C語言實例

    TMS320LF5402 C語言實例源代碼分享
    發(fā)表于 05-23 18:21 ?16次下載

    C語言程序設計及應用實例

    其他編程語言——C語言程序設計及應用實例,感興趣的小伙伴可以看一看。
    發(fā)表于 11-03 15:50 ?0次下載

    C語言入門教程之C語言編程實例源代碼資料免費下載

    本文檔的主要內(nèi)容詳細介紹的是C語言入門教程之C語言編程實例源代碼資料免費下載。
    發(fā)表于 12-06 08:00 ?35次下載

    使用C語言從視頻截圖的方法實例程序說明

    本文檔的主要內(nèi)容詳細介紹的是使用C#從視頻截圖的方法實例程序說明。
    發(fā)表于 11-01 17:29 ?3次下載

    使用單片機點亮多個LED燈的方法C語言程序實例免費下載

    本文檔的主要內(nèi)容詳細介紹的是使用單片機點亮多個LED燈的方法C語言程序實例免費下載。
    發(fā)表于 11-06 17:11 ?17次下載

    設計模式:簡單工廠模式——基于C語言

    設計模式:簡單工廠模式——基于C語言背景 看了劉偉、胡志剛的《C#設計模式(第二版)》——清華大學出版社,利用里面闡述的
    發(fā)表于 01-13 13:45 ?6次下載
    設計模式:<b class='flag-5'>簡單</b><b class='flag-5'>工廠</b>模式——基于<b class='flag-5'>C</b><b class='flag-5'>語言</b>

    詳解ADI智能工廠方

    — ADI 智能工廠方案》在線培訓課程,由ADI亞太區(qū)工業(yè)自動化行業(yè)市場部經(jīng)理于常濤講解ADI 智能工廠方案。
    的頭像 發(fā)表于 01-21 10:48 ?1983次閱讀

    使用簡單工廠方法實現(xiàn)硬盤對象創(chuàng)建

    簡單工廠方法定義一個用于創(chuàng)建對象的類,該類接受一個參數(shù),通過參數(shù)決定創(chuàng)建不同的對象。
    的頭像 發(fā)表于 05-14 14:07 ?1501次閱讀

    C語言簡單工廠方法編程案例解析

    硬盤就是要創(chuàng)建的對象(即:產(chǎn)品)。為了讓不同供應商提供的硬盤可以通用,要定義一個硬盤產(chǎn)品類,并讓不同供應商的硬盤都繼承硬盤產(chǎn)品類的接口。
    發(fā)表于 04-05 10:41 ?590次閱讀

    c語言程序實例大全

    電子發(fā)燒友網(wǎng)站提供《c語言程序實例大全.rar》資料免費下載
    發(fā)表于 11-20 11:39 ?1次下載
    <b class='flag-5'>c</b><b class='flag-5'>語言</b>程序<b class='flag-5'>實例</b>大全