当前位置:首页 > > 嵌入式大杂烩
[导读]我们接简单工厂方法模式_C语言实现中生产电脑的模拟场景来看。


简介

我们接简单工厂方法模式_C语言实现中生产电脑的模拟场景来看。

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

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

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

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

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

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

硬盘对象创建

参与者

1、Product: HardDisk

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

2、Concrete Product: SeagateHardDisk, ToshibaHardDisk

实现不同供应商的硬盘(与简单工厂方法相同)

3、Factory: HardDiskFactory

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

4、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 硬盘


免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭