• <center id="q6uyy"><td id="q6uyy"></td></center>
    <dd id="q6uyy"></dd>
  • 推廣 熱搜: csgo  vue  angelababy  2023  gps  新車  htc  落地  app  p2p 

    C語言也有面向對象里的簡單工廠,一起來看看

       2023-07-25 網絡整理佚名1210
    核心提示:簡單工廠方法定義一個用于創建對象的類,該類接受一個參數,通過參數決定創建不同的對象。為了體現簡單工廠方法和工廠方法的區別和聯系,此處把簡單工廠方法先單獨講一下。此處列出多個部件是為了后面講解工廠方法、抽象工廠方法時使用同一個模擬場景。硬盤就是要創建的對象(即:產品)。還需要定義一個創建硬盤對象的類(即:工廠)。

    1 簡介

    簡單工廠方法定義了一個用于創建對象的類,該類接受一個參數,通過參數決定不同對象的創建。

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

    為了體現簡單工廠方法和工廠方法之間的區別和聯系,這里將簡單工廠方法單獨討論。

    2、模擬場景

    假設你要生產計算機,它由硬盤、內存條、CPU和主板組成。 為了確??煽康墓?,您為每個組件選擇了至少兩個供應商。 例如:

    硬盤供應商,

    內存供應商,

    CPU供應商intel、AMD

    主板供應商intel、AMD

    這里列出多個組件是為了后面解釋工廠方法和抽象工廠方法時使用相同的模擬場景。 本章中,簡單工廠方法暫時不需要涉及那么多組件,所以僅以硬盤為例進行說明。

    3、實現思路

    硬盤是要創建的對象(即:產品)。 為了讓不同供應商提供的硬盤能夠通用,需要定義一個硬盤產品類,讓不同供應商的硬盤繼承該硬盤產品類的接口。

    還需要定義一個用于創建硬盤對象的類(即:工廠)。 工廠類根據參數決定創建哪個廠商的硬盤對象。

    4、實現硬盤對象的創建

    參加者:

    (1):定義硬盤對象的接口

    (2): ,實現不同供應商的硬盤

    (3):根據參數,創建不同供應商的硬盤對象

    統一建模語言:

    代碼示例:

    。H:

    #ifndef HARD_DISK_H
    #define HARD_DISK_H
    struct HardDisk {
        void (*Operation)(struct HardDisk *this);
    };
    #endif

    代碼示例:

    。H:

    #ifndef SEAGATE_HARD_DISK_H
    #define SEAGATE_HARD_DISK_H
    #include "hard_disk.h"
    struct SeagateHardDisk {
        struct HardDisk hardDisk;
    };
    // 構造函數
    void SeagateHardDisk(struct SeagateHardDisk *this);
    // 析構函數
    void _SeagateHardDisk(struct SeagateHardDisk *this);
    #endif

    。C:

    #include "seagate_hard_disk.h"
    #include "stdio.h"
    void SeagateOperation(struct SeagateHardDisk *this)
    {
        printf("這是 Seagate 硬盤\n");
    }
    void SeagateHardDisk(struct SeagateHardDisk *this)
    {
        this->hardDisk.Operation = (void(*)(struct HardDisk *))SeagateOperation;
    }
    void _SeagateHardDisk(struct SeagateHardDisk *this)
    {
        this->hardDisk.Operation = NULL;
    }

    代碼示例:

    。H:

    #ifndef TOSHIBA_HARD_DISK_H
    #define TOSHIBA_HARD_DISK_H
    #include "hard_disk.h"
    struct ToshibaHardDisk {
        struct HardDisk hardDisk;
    };
    // 構造函數
    void ToshibaHardDisk(struct ToshibaHardDisk *this);
    // 析構函數
    void _ToshibaHardDisk(struct ToshibaHardDisk *this);
    #endif

    。C:

    #include "toshiba_hard_disk.h"
    #include "stdio.h"
    void ToshibaOperation(struct ToshibaHardDisk *this)
    {
        printf("這是 Toshiba 硬盤\n");
    }
    void ToshibaHardDisk(struct ToshibaHardDisk *this)
    {
        this->hardDisk.Operation = (void(*)(struct HardDisk *))ToshibaOperation;
    }
    void _ToshibaHardDisk(struct ToshibaHardDisk *this)
    {
        this->hardDisk.Operation = NULL;
    }

    代碼示例:

    。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);
    };
    // 構造函數
    void HardDiskFactory(struct HardDiskFactory *this);
    // 析構函數
    void _HardDiskFactory(struct HardDiskFactory *this);
    #endif

    。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\n");
                    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\n");
                    return NULL;
                }
                ToshibaHardDisk(toshibaHardDisk);
                return (struct HardDisk *)toshibaHardDisk;
            }
            default:
                printf("未知的供應商\n");
                return NULL;
        }
    }
    void Destroy(struct HardDiskFactory *this, struct HardDisk* hardDisk)
    {
        if (hardDisk != NULL) {
            free(hardDisk);
        }
    }
    // 構造函數
    void HardDiskFactory(struct HardDiskFactory *this)
    {
        this->Create = Create;
        this->Destroy = Destroy;
    }
    // 析構函數
    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);
        // 創建 seagate 硬盤對象
        hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_SEAGATE);
        // 使用 seagate 硬盤對象
        hardDisk->Operation(hardDisk);  
        // 銷毀 seagate 硬盤對象
        hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);       
        // 創建 toshiba 硬盤對象
        hardDisk = hardDiskFactory.Create(&hardDiskFactory, HARD_DISK_SUPPLIER_TOSHIBA);
        // 使用 seagate 硬盤對象
        hardDisk->Operation(hardDisk);
        // 銷毀 toshiba 硬盤對象
        hardDiskFactory.Destroy(&hardDiskFactory, hardDisk);    
        _HardDiskFactory(&hardDiskFactory);
    }

    客戶端顯示示例:

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

     
    反對 0舉報 0 收藏 0 打賞 0評論 0
     
    更多>同類資訊
    推薦圖文
    推薦資訊
    點擊排行
    網站首頁  |  關于我們  |  聯系方式  |  使用協議  |  版權隱私  |  網站地圖  |  排名推廣  |  廣告服務  |  積分換禮  |  網站留言  |  RSS訂閱  |  違規舉報
    Powered By DESTOON
     
    三级精品影视国产,欧美乱伦免费综合,亚洲a在线中文,人妻色综合网站
  • <center id="q6uyy"><td id="q6uyy"></td></center>
    <dd id="q6uyy"></dd>