03 디자인 패턴 _ 팩토리 방식 패턴 _C 언어 구현

공장 방법 패턴

소개

"간단한 공장 방식"에서 생산 컴퓨터의 시뮬레이션 장면을 살펴 보겠습니다.

간단한 공장 방법에서는 HardDiskFactory에서 여러 공급 업체의 하드 디스크를 만듭니다. 이 접근 방식에는 몇 가지 문제가 있습니다.

  1. 공급 업체가 많으면 HardDiskFactory는 "대규모 범주"가됩니다.

  2. 새로운 하드 디스크 공급 업체가 도입 될 것이라고 가정하면 HardDiskFactory의 Create 메소드에 케이스 분기를 추가해야합니다.
    Create 메소드에서 케이스 분기를 추가하는 관행은 "개폐 원칙"을 위반합니다.

지금 이야기 할 "공장 방법"은 위의 문제를 해결할 수 있습니다.

공장 출하 방법은 각 공급 업체의 하드 드라이브에 대한 공장을 제공합니다. 다른 팩토리 개체를 통해 다른 제품 개체를 만듭니다. 즉, 팩토리 메서드는 개체를 만드는 데 사용되는 클래스를 직접 정의하지 않고 하드 디스크 개체를 만들기위한 인터페이스를 정의하고 하위 클래스가 인스턴스화 할 공급 업체의 하드 디스크를 결정하도록합니다.

새 하드 디스크 공급 업체를 추가 할 때 하나의 공장 개체 만 추가하면됩니다. 원래 공장에서 코드를 추가 할 필요가 없기 때문에 "개폐 원칙"을 위반하지 않으며 공급 업체의 지속적인 증가로 인한 "대규모 카테고리"가 없습니다.

공장 방법을 사용하여 하드 디스크 개체 만들기

참가자

  1. 제품 : 하드 디스크

하드 디스크 개체의 인터페이스를 정의합니다 (간단한 공장 방법과 동일).

  1. 구체적인 제품 : SeagateHardDisk, ToshibaHardDisk

다양한 공급 업체의 하드 드라이브 구현 (간단한 공장 방식과 동일)

  1. 공장 : HardDiskFactory

하드 디스크 팩토리 메서드를 선언하면 특정 하드 디스크 개체의 생성이 해당 하위 클래스 ConcreteFactory로 넘겨집니다.

  1. 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 硬盘

추천

출처blog.csdn.net/weixin_46826913/article/details/106453059