设计模式之工厂模式

设计模式

设计模式是软件开发人员在软件开发过程中面临的一般开发问题的解决方案。设计模式通常描述了一组相互紧密作用的类和对象;设计模式有23种;使代码更容易被他人理解,保证代码可靠性,程序的重要性。是通过代码设计经验总结而成,稳定性,拓展性更强。

工厂模式

工厂模式(Factory Pattern)是最常用的设计模式之一;这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式;在工厂模式中,创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

结构体的新玩法

// 一个小例子,没有采用工厂模式
#include <stdio.h>

struct Animal{
  	char name[128];
    int age;
    int sex;
    int others;
    void (*peat)();
    void (*beat)();
    void (*test)();
};

void dogEat()
{
    printf("狗吃骨头\n");
}

void catEat()
{
    printf("猫吃鱼\n");
}

void dogBeat()
{
    printf("咬\n");
}

void catBeat()
{
    printf("挠\n");
}

int main()
{
    struct Animal dog = {
        .peat = dogEat,
        .beat = dogBeat
    };
    struct Animal cat = {
        .peat = catEat,
        .beat = catBeat
    };
    
    dog.peat();
    cat.peat();
    
    dog.beat();
    cat.beat();
}

采用工厂模式实现

按照需求一共创建了4个文件,包括3个 .c 文件和1个 .h文件(作为自己对工厂模式的入门学习)

// 此文件为 .c 文件
#include "animal.h"
#include <string.h>

struct Animal* findFromDefine(char* buf, struct Animal *head)
{

    if(head == NULL)
    {
        printf("空\n");
        return NULL;
    }
    else
    {
        while(head != NULL)
        {
            if(strcmp(buf, head->name) == 0)
            {
                return head;
            }
            head = head->next;
        }
        return NULL;
    }
}

int main()
{
    char buf[128] = {'\0'};

    struct Animal* phead = NULL;
    struct Animal* tmp = NULL;

    phead = putCatInLink(phead);
    phead = putDogInLink(phead);
    
    while(1)
    {
        printf("Input:mycat or mydog\n");
        scanf("%s", buf);

        tmp = findFromDefine(buf, phead);

        if(tmp != NULL)
        {
            tmp->peat();
            tmp->beat();
        }
        memset(buf, '\0', sizeof(buf));
    }

    return 0;
}
// cat.c
#include "animal.h"

void catEat()
{
    printf("猫吃鱼\n");
}

void catBeat()
{
    printf("挠\n");
}
    
struct Animal cat = {
    .name = "mycat",
    .peat = catEat,
    .beat = catBeat
};

struct Animal* putCatInLink(struct Animal *phead)
{
    if(phead == NULL)
    {
        phead = &cat;
        return phead;
    }
    else
    {
        cat.next = phead;
        phead = &cat;
        return phead;
    }
}
// dog.c
#include "animal.h"

void dogEat()
{
    printf("狗吃骨头\n");
}

void dogBeat()
{
    printf("咬\n");
}

struct Animal dog = {
    .name = "mydog",
    .peat = dogEat,
    .beat = dogBeat
};

struct Animal* putDogInLink(struct Animal* phead)
{
    if(phead == NULL)
    {
        phead = &dog;
        return phead;
    }
    else
    {
        dog.next = phead;
        phead = &dog;
        return phead;
    }
}
// animal.h
#include <stdio.h>

struct Animal{
  	char name[128];
    int age;
    int sex;
    int others;
    void (*peat)();
    void (*beat)();
    void (*test)();
    struct Animal *next;
};

struct Animal* putDogInLink(struct Animal* phead);
struct Animal* putCatInLink(struct Animal* phead);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Luish Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值