设计模式的C语言实现--工厂模式

简单工厂方法模式
1 简介
简单工厂方法定义一个用于创建对象的类,该类接受一个参数,通过参数决定创建不同的对象。

GOF并没有把简单工厂方法定义为23种设计模式之一,可以认为简单工厂方法是工厂方法的简化形式。

为了体现简单工厂方法和工厂方法的区别和联系,此处把简单工厂方法先单独讲一下。

2 模拟场景
假设你要生产电脑,电脑由硬盘、内存条、CPU、主板的部件组成。你为了保证供应链可靠,每种部件都选择了至少两家供应商。比如:

硬盘供应商 seagate、Toshiba

内存条供应商 SAMSUNG、Crucial

CPU供应商 intel、AMD

主板供应商 intel、AMD

此处列出多个部件是为了后面讲解工厂方法、抽象工厂方法时使用同一个模拟场景。本章讲简单工厂方法暂时不需要涉及这么多部件,所以仅以硬盘这一个部件为例进行讲解。

3 使用简单工厂方法实现的思路
硬盘就是要创建的对象(即:产品)。为了让不同供应商提供的硬盘可以通用,要定义一个硬盘产品类,并让不同供应商的硬盘都继承硬盘产品类的接口。

还需要定义一个创建硬盘对象的类(即:工厂)。工厂类根据参数决定创建哪家供应商的硬盘对象。

4 使用简单工厂方法实现硬盘对象创建
参与者

Product: HardDisk
定义硬盘对象的接口

Concrete Product: SeagateHardDisk, ToshibaHardDisk
实现不同供应商的硬盘

SimpleFactory: HardDiskFactory
根据参数,创建不同供应商的硬盘对象

UML

在这里插入图片描述

在这里插入图片描述

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;
};

// 构造函数
void SeagateHardDisk(struct SeagateHardDisk *this);

// 析构函数
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 硬盘\n");
}

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;
};

// 构造函数
void ToshibaHardDisk(struct ToshibaHardDisk *this);

// 析构函数
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 硬盘\n");
}

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);
};

// 构造函数
void HardDiskFactory(struct HardDiskFactory *this);

// 析构函数
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\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 硬盘

 

工厂方法模式
简介
我们接着“简单工厂方法”中生产电脑的模拟场景来看。

在简单工厂方法中,不同供应商的硬盘都由HardDiskFactory创建出来。这种做法存在几个问题:

当供应商很多时,HardDiskFactory就会成为一个“过大类”

假设现在要新引入一家硬盘供应商,那就需要在HardDiskFactory的Create方法中增加一个case分支。
在Create方法中增加case分支的做法违反了“开闭原则”。

我们现在要讲的“工厂方法”可以解决上述问题:

工厂方法针对每一个供应商的硬盘提供一个工厂。通过不同工厂对象来创建不同的产品对象。也就是说,工厂方法不直接定义用于创建对象的类,而是定义一个创建硬盘对象的接口,让子类决定实例化哪一个供应商的硬盘。

当新增一个硬盘供应商时,只需要新增一个工厂对象。不需要在原有工厂中增加代码,所以不违反“开闭原则”,也不会因为不断增加供应商而产生“过大类”。

使用工厂方法实现硬盘对象创建
参与者

Product: HardDisk
定义硬盘对象的接口(与简单工厂方法相同)

Concrete Product: SeagateHardDisk, ToshibaHardDisk
实现不同供应商的硬盘(与简单工厂方法相同)

Factory: HardDiskFactory
声明硬盘工厂方法,具体硬盘对象的创建交给其子类 ConcreteFactory

ConcreteFactory: SeagateHardDiskFactory, ToshibaHardDiskFactory
创建具体硬盘对象

UML

在这里插入图片描述

HardDisk、SeagateHardDisk、ToshibaHardDisk示例代码与简单工厂方法完全一致,此处不再重复

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;
};

// 构造函数
void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this);

// 析构函数
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;
    }    
}

// 构造函数
void SeagateHardDiskFactory(struct SeagateHardDiskFactory *this)
{
    this->hardDiskFactory.Create = SeagateCreate;
    this->hardDiskFactory.Destroy = SeagateDestroy;
}

// 析构函数
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;
};

// 构造函数
void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this);

// 析构函数
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;
    }    
}

// 构造函数
void ToshibaHardDiskFactory(struct ToshibaHardDiskFactory *this)
{
    this->hardDiskFactory.Create = ToshibaCreate;
    this->hardDiskFactory.Destroy = ToshibaDestroy;
}

// 析构函数
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;
    // 创建 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;
    // 创建 toshiba 硬盘对象
    hardDisk = hardDiskFactory->Create(hardDiskFactory);
    // 使用 seagate 硬盘对象
    hardDisk->Operation(hardDisk);
    // 销毁 toshiba 硬盘对象
    hardDiskFactory->Destroy(hardDiskFactory, hardDisk);
    _ToshibaHardDiskFactory(&toshibaHardDiskFactory);
}


客户端显示示例

./hard_disk
这是 Seagate 硬盘
这是 Toshiba 硬盘
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值