本文旨在探讨,利用C语言实现工厂模式。

工厂模式:工厂模式是一种面向对象的模式,用类与对象实现面向对象模式。C语言则用结构体实现,类似分文件编程。

类与对象:类是模板,对应结构体的数据类型和函数指针,用于实现功能。对象,是类的体现,即结构体数据赋值,函数实现。

构建工厂:构建结构体(类),依据结构体实现参数,函数实现(实例化对象),构建链表,查询链表实现指定对象函数调用。

代码实现

        1.构建animal类(名字,eat功能)

        2.实例化出cat,dog,people对象

        3.查询链表,实现指定对象的eat调用

animal.h 

struct animal
{
        char name[8];
        void (*eat)();

        struct animal *next;
};

struct animal *cat_into_link(struct animal *head);
struct animal *dog_into_link(struct animal *head);
struct animal *people_into_link(struct animal *head);

cat.c

#include <stdio.h>
#include "animal.h"

void cat_eat()
{
        printf("cat eating\n");
}

struct animal cat = 
{
        .name = "cat",
        .eat = cat_eat
};

struct animal *cat_into_link(struct animal *head)
{
        if(head == NULL)
        {
                head = &cat;
                return head;
        }
        else
        {
                 cat.next = head;
                 head = &cat;
                 return head;
        }
}

dog.c

#include <stdio.h>
#include "animal.h"

void dog_eat()
{
        printf("dog eating\n");
}

struct animal dog = 
{
        .name = "dog",
        .eat = dog_eat
};

struct animal *dog_into_link(struct animal *head)
{
        if(head == NULL)
        {
                head = &dog;
                return head;
        }
        else
        {
                 dog.next = head;
                 head = &dog;
                 return head;
        }
}

people.c

#include <stdio.h>
#include "animal.h"

void people_eat()
{
        printf("people eating\n");
}

struct animal people = 
{
        .name = "people",
        .eat = people_eat
};

struct animal *people_into_link(struct animal *head)
{
        if(head == NULL)
        {
                head = &people;
                return head;
        }
        else
        {
                 people.next = head;
                 head = &people;
                 return head;
        }
}

main.c

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

char name[8] = {'\0'};

struct animal *find_name_into_link(struct animal *head)
{
        struct animal *temp = head;
        if(head == NULL)
        {
                return NULL;
        }
        else
        {
                while(temp != NULL)
                {
                        if(strcmp(temp->name,name) == 0)
                        {
                                head = temp;
                                return head;
                        }
                        temp = temp->next;
                }
        }
        return NULL;
}

int main()
{
        struct animal *head = NULL;
        struct animal *ret = NULL;
        head = cat_into_link(head);
        head = dog_into_link(head);
        head = people_into_link(head);

        while(1)
        {
                printf("input cat ,dog, people:\n");
                scanf("%s",name);
                if(strcmp(name,".q") == 0)
                {
                        break;
                }
                ret = find_name_into_link(head); 
                memset(name,'\0',sizeof(name));
                ret->eat();
                ret = NULL;
        }
        return 0;
}

结果示例

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐