C语言实现简单工厂模式

  • 工厂模式是什么

  • 类与对象

  • 构造工厂模式

  • 实现工厂模式

一、工厂模式是什么?

工厂模式是一种面向对象的模式,是23种里面的一种,c语言是面向过程,但我们可以用类与对象(结构体)实现一个简单的面向对象模式,跟分文件编程很像。

二、类与对象

类可以说是一种模板,结构体里面的数据类型,用于实现很多功能,对象,即类的一种体现,我给结构体赋值,调用。

三、构造工厂模式

对于添加功能函数的解析

 用链表中的尾插法来实现main函数和各种功能的连接 

struct animal*PutLink_cat(struct animal * head)
{
        struct animal * lost;
        lost = head;

        if(head == NULL)
        {
                head = &cat;

        }

        else
        {
                while(lost->next != NULL)
                {
                        lost = lost->next;
                }

                lost->next = &cat;
        }

        return head;
}

animal.h

#include <stdio.h>

struct animal     //定义结构体里面的功能
{
        char name[20];
        int age;
        void (*eat)(void);
        struct animal*next;
};


struct animal * PutLink_cat(struct animal * head);//把功能添加到链表去
struct animal * PutLink_dog(struct animal * head);
struct animal * PutLink_fish(struct animal * head);

main.c

#include "animal.h"
#include <string.h>
#include <stdlib.h>

struct animal * findAnimal(struct animal * head,char buf[128])//查找链表中的功能
{
        struct animal * lost;
        lost = head;

        if(lost == NULL)//如果链表里面没有功能,就退出程序
        {
                printf("no find....\n");
                exit(-1);
        }
        else
        {
                while(lost != NULL)
                {
                        if(strcmp(lost->name,buf)==0)//如果这个功能和我们输入的关键词一样
                        {
                                return lost;//就返回这个功能
                        }


                        lost = lost->next;//如果第一次没找到功能,我们就遍历下一个节点,在找一次
                }

                return NULL;

        }
}


void main()
{
        char buf[128];
        struct animal * head = NULL;
        struct animal * lost = NULL;

        head = PutLink_cat(head);//创建链表
        head = PutLink_dog(head);

        while(1)
        {
                printf("plase input you find animal:");
                scanf("%s",buf);

                lost = findAnimal(head,buf);//查找功能,返回功能的地址
                if(lost != NULL)//如果有这个功能,就打开对应的功能
                {
                        printf("animal age = %d\n",lost->age);
                        lost->eat();

                }
                else
                {
                        printf("no animal...\n");
                }

                memset(buf,'\0',sizeof(buf));//每执行一次,清零数组
        }

}
~    

cat.c

#include "animal.h"

void catEat(void) //要实现的功能
{
        printf("cat eat fish!\n");
}

struct animal cat = { 

        .name = "cat",//用于查找功能时候的索引
        .age  = 10,
        .eat  = catEat

};

struct animal*PutLink_cat(struct animal * head)//用于添加功能
{
        struct animal * lost;
        lost = head;

        if(head == NULL)//尾插法,如果头指针没有节点,那我就是第一个节点
        {
                head = &cat;

        }

        else    //如果有节点,我就挂在最后一个节点
        {
                while(lost->next != NULL)
                {
                        lost = lost->next;
                }

                lost->next = &cat;
        }

        return head;
}

dog.c

#include "animal.h"

void Dogeat(void)
{

        printf("dog eat shift\n");
}


struct animal dog = {

        .name = "dog",
        .age  = 11,
        .eat  = Dogeat
};


struct animal * PutLink_dog(struct animal* head)
{
        struct animal * lost;
        lost = head;

        if(head == NULL)
        {
                head = &dog;
        }

        else
        {
                while(lost -> next != NULL)
                {
                        lost = lost->next;
                }

                lost->next = &dog;
        }

        return head;
}

实现效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

No Iverson

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

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

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

打赏作者

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

抵扣说明:

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

余额充值