C语言简单工厂模式和工程创建 您所在的位置:网站首页 c语言究竟是什么 C语言简单工厂模式和工程创建

C语言简单工厂模式和工程创建

2023-04-01 01:23| 来源: 网络整理| 查看: 265

一,设计模式概念引入 ① 什么是设计模式 设计模式通常被面向对象的软件开发人员所采用,是软件开发人员在软件开发过程中面临问题的解决方案。也是众多软件开发人员经过长时间的试验和错误总结出来的23种设计模式。虽然很多人认为C语言只是面向过程的开发,但鄙人认为此说法并不全面,难道面向对象的语言就不面向过程了吗?同理,在C语言的使用过程中一样允许面向对象。在平时代码编写中,有时会遇到修改一处BUG而影响整个代码的运行,设计模式的出现正好就解决了这一问题。

② 什么是类和对象

C语言

面向过程

也可以面向对象,还能用其设计模式,只是不太友好的面向对象

Java

面向对象

也可以面向过程,只是不太友好的面向过程

面向过程和面向对象只是代码的一种编程方式和编程思想,不应该和编程语言绑定起来

类:类是面对对象程序设计实现信息封装的基础。类是一种用户定义的引用数据类型,也称类类型。比如C语言中结构体,就是用户自己定义的一种数据类型

struct Animal{ int age; int sex; //成员属性 void (*peat)(); //函数指针 void (*pbeat)(); //成员方法 };

对象:类的一种具象。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例就称为对象。

dog是类Animal的一种具体对象;

● cat是类Animal的一种具体对象;

● person是类Animal的一种具体对象;

struct Animal dog; struct Animal cat; struct Animal person;

③ 什么是工厂模式

工厂模式是最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的(最佳)方式;

● 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象;● 共同的接口:我们自己创建共同接口API,从工厂里面获得需要的对象放入main函数中去运行。

二,C语言工厂模式的实现 ① 普通类和对象的代码实现 #include /*类:struct Animal*/ /*对象: struct Animal dog/cat/person*/ struct Animal { char name[128]; int age; int sex; //成员属性 void (*peat)(); void (*pbeat)(); //成员方法 }; void dogeat() { printf("狗吃骨头\n"); } void cateat() { printf("猫吃鱼\n"); } void personeat() { printf("人吃饭\n"); } void dogbeat() { printf("狗咬人\n"); } void catbeat() { printf("猫挠人\n"); } void personbeat() { printf("打人\n"); } int main() { struct Animal dog = { .peat = dogeat, //部分成员赋值 .pbeat = dogbeat }; struct Animal cat = { .peat = cateat, .pbeat = catbeat }; struct Animal person = { .peat = personeat, //对象,事物的具象 .pbeat = personbeat }; dog.peat(); cat.peat(); person.peat(); dog.pbeat(); cat.pbeat(); person.pbeat(); return 0; }

② 工厂模式代码实现

● 工厂模式不会暴露创建对象的代码逻辑,上面对象代码逻辑全部暴露无遗;

● 以上面的代码为例,以工厂模式的方式将dog,cat,person这几个对象拆分成单独的代码文件,并在每个文件中提供用来链接的函数,方便main函数的调用;

如上图中,左侧可以比拟为一个工厂,工厂里有很多个可能,右侧可比拟一个main函数的入口,当我们用到哪个功能时, 就在main函数中调用工厂里的功能接口(API)就可以了,如果想要在工厂里建立一个新功能时,直接新添加一个文件就好,工厂中一个独立的文件就代表是一个独立的功能,各个功能之间互不干扰,比如你改错了一个功能文件,但不影响其他的功能文件。

分别创建需要的文件:

● cat.c #include "animal.h" void cateat() { printf("猫吃鱼\n"); } void catbeat() { printf("猫挠人\n"); } struct Animal cat = { .name = "Tom", .peat = cateat, .pbeat = catbeat }; struct Animal *putCatInLink(struct Animal *phead) //头插法,会改变头,所以用到指针 { //链表头传过来 if(phead == NULL) //如果头为空,那就返回cat为链表头 { return &cat; } else { cat.next = phead; //否则cat的下一个为链头 phead = &cat; //再把链头指向猫 return phead; } };

● dog.c #include "animal.h" void dogeat() { printf("狗吃骨头\n"); } void dogbeat() { printf("狗咬人人\n"); } struct Animal dog = { .name = "huang", .peat = dogeat, .pbeat = dogbeat }; struct Animal *putDogInLink(struct Animal *phead) //头插法,会改变头,所以用到指针 { //链表头传过来 if(phead == NULL) //如果头为空,那就返回cat为链表头 { return &dog; } else { dog.next = phead; //否则cat的下一个为链头 phead = &dog; //再把链头指向猫 return phead; } }; ● person.c #include "animal.h" void personeat() { printf("人吃饭\n"); } void personbeat() { printf("揍人\n"); } struct Animal person = { .name = "xiaomei", .peat = personeat, //对象,事务的具象 .pbeat = personbeat }; struct Animal* putPersonInLink(struct Animal *phead) { if(phead == NULL){ return &person; }else{ person.next = phead; phead = &person; return phead; } };

● animal.h #include struct Animal{ char name[128]; int age; int sex; //成员属性 void (*peat)(); void (*pbeat)(); //成员方法 struct Animal *next; //涉及到链表,添加链表节点 }; struct Animal *putCatInLink(struct Animal *phead); //cat.c文件中的该函数将cat相关全局的结构体加到链表中 struct Animal *putDogInLink(struct Animal *phead); //dog.c文件中的该函数将cat相关全局的结构体加到链表中 struct Animal *putPersonInLink(struct Animal *phead); //person.c文件中的该函数将cat相关全局的结构体加到链表中

● mainpro.c #include "animal.h" int main() { struct Animal *phead = NULL; //初始化 phead = putCatInLink(phead); phead = putDogInLink(phead); phead = putPersonInLink(phead); return 0; }

编译说明: 多个.c文件同时编译:gcc *.c , 如果编译过程中没有报错,但是没有输出内容,是因为在manpro.c的代码中没有输出代码,但是链接成功

● 完善mainpro.c #include "animal.h" #include struct Animal *fileName(char *str,struct Animal *phead) { struct Animal *tmp = phead; if(tmp == NULL) { printf("空\n"); return NULL; } else { while(tmp != NULL) //遍历链表 { if(strcmp(tmp->name,str) == 0) //tmp的值和输入的名字是想等的,说明找到了 { return tmp; } tmp = tmp -> next; //没找到的话就继续遍历链表 } return NULL; //一直没找到就返回NULL; } } int main() { char buf[128] = {"\0"}; struct Animal *phead = NULL; //初始化 struct Animal *ptmp; phead = putCatInLink(phead); phead = putDogInLink(phead); phead = putPersonInLink(phead); while(1) { printf("请输入: Tom,huang,xiaomei\n"); scanf("%s",buf); ptmp = fileName(buf,phead); if(ptmp != NULL) { ptmp -> pbeat(); ptmp -> peat(); } memset(buf,'\0',sizeof(buf)); } return 0; }

编译结果: C语言简单工厂模式完成!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有