単純なファクトリメソッドパターン
1はじめに
単純なファクトリメソッドは、オブジェクトを作成するためのクラスを定義します。クラスはパラメータを受け取り、パラメータを介してさまざまなオブジェクトを作成することを決定します。
GOFは、単純なファクトリーメソッドを23の設計パターンの1つとして定義していません。単純なファクトリーメソッドは、ファクトリーメソッドの単純化された形式と見なすことができます。
シンプルファクトリメソッドとファクトリメソッドの違いと接続を反映するために、ここではシンプルファクトリメソッドについて個別に説明します。
2シミュレーションシーン
コンピューターを作成するとします。コンピューターは、ハードディスク、メモリースティック、CPU、マザーボードなどのパーツで構成されています。信頼性の高いサプライチェーンを確保するために、コンポーネントごとに少なくとも2つのサプライヤーを選択しました。といった:
ハードドライブサプライヤーSeagate、東芝
メモリサプライヤーSAMSUNG、Crucial
CPUサプライヤーIntel、AMD
マザーボードサプライヤーインテル、AMD
ファクトリーメソッドと抽象ファクトリーメソッドを後で説明するときに同じシミュレーションシーンを使用するために、複数のコンポーネントがここにリストされています。この章では、単純なファクトリーメソッドはそれほど多くのコンポーネントを含む必要がないため、ハードディスクのみを例として使用して説明します。
3単純なファクトリメソッドを使用するアイデア
ハードディスクは作成されるオブジェクトです(例:製品)。異なるベンダーが提供するハードディスクをユニバーサルにするためには、ハードディスク製品カテゴリーを定義し、異なるベンダーのハードディスクがハードディスク製品カテゴリーのインターフェースを継承できるようにする必要があります。
また、ハードディスクオブジェクトを作成するためのクラス(つまり、ファクトリ)を定義する必要があります。ファクトリクラスは、パラメータに従って、作成するサプライヤのハードディスクオブジェクトを決定します。
4単純なファクトリメソッドを使用してハードディスクオブジェクトを作成する
参加者
- 製品:HardDisk
ハードディスクオブジェクトのインターフェイスを定義する
- コンクリート製品: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 硬盘