1、简单工厂模式
工厂模式是面向对象语言的20多种的设计模式之一,也是比较常用,虽然C语言是一个面向过程的语言,但通过结构体与指针也能实现一些简单的面向对象。基于此,玩了一下C语言简单工厂模式。
工厂模式:https://www.runoob.com/design-pattern/factory-pattern.html
2、人话简单工厂模式
图解:
通俗点来讲就是分文件编程,跟在keil里面编写STM32或51差不多,将文件分为了 .h文件与.c文件,然后在主函数里调用相应的函数。
3、构造工厂
图解:
上图其实就是建立了一个Animal工厂,里面含有相应的 .h与.c文件,需要的添加新的产品,只需添加相应的.h与.c即可。
4、构造产品类、属性、方法
属性与方法:
#include <stdio.h>
struct Animal{
char name[128];
int age; //属性
int sex;
void (*peat)();
void (*pbeat)();//方法
struct Animal* next;//链表指针
};
构造类:
#include "Animal.h"
void cat_eat()
{
printf("cat eating fish.....\n");
}
void cat_beat()
{
printf("cat beat small jiji....\n");
}
struct Animal cat={ //构造类
.name="TOM",
.age=8,
.peat=cat_eat,
.pbeat=cat_beat
};
5、动态链表链接函数
动态链表尾插法:
struct Animal* putCatLink(struct Animal* head)
{
struct Animal* point;
point=head;
if(head== NULL){
head=&cat;
}else{
while(point->next != NULL){
point=point->next;
}
point->next=&cat;
}
6、实现简单工厂模式
简单工厂模式整体代码图样:
整体代码:
Animal.h
#include <stdio.h>
struct Animal{
char name[128];
int age;
int sex;
void (*peat)();
void (*pbeat)();
struct Animal* next;
};
cat.c
#include "Animal.h"
void cat_eat()
{
printf("cat eating fish.....\n");
}
void cat_beat()
{
printf("cat beat small jiji....\n");
}
struct Animal cat={
.name="TOM",
.age=8,
.peat=cat_eat,
.pbeat=cat_beat
};
struct Animal* putCatLink(struct Animal* head)
{
struct Animal* point;
point=head;
if(head== NULL){
head=&cat;
}else{
while(point->next != NULL){
point=point->next;
}
point->next=&cat;
}
return head;
}
cat.h
struct Animal* putCatLink(struct Animal* head);
dog.c
#include "Animal.h"
void dog_eat()
{
printf("dog eating shi......\n");
}
void dog_beat()
{
printf("dog beat yours jiji......\n");
}
struct Animal dog={
.name="JACK",
.age=10,
.peat=dog_eat,
.pbeat=dog_beat
};
struct Animal* putDogLink(struct Animal* head)
{
struct Animal* point;
point=head;
if(head== NULL){
head=&dog;
}else{
while(point->next != NULL){
point=point->next;
}
point->next=&dog;
}
return head;
}
dog.h
struct Animal* putDogLink(struct Animal* head);
person.c
include "Animal.h"
void person_eat()
{
printf("person eating rice.....\n");
}
void person_beat()
{
printf("person beating yours xiaojiji.....\n");
}
struct Animal person={
.name="dugad",
.age=22,
.peat=person_eat,
.pbeat=person_beat
};
struct Animal* putPersonLink(struct Animal* head)
{
struct Animal* point;
point=head;
if(head== NULL){
head=&person;
}else{
while(point->next != NULL){
point=point->next;
}
point->next=&person;
}
return head;
}
person.h
struct Animal* putPersonLink(struct Animal* head);
mainPro.c
#include <string.h>
#include "Animal.h"
#include "cat.h"
#include "dog.h"
#include "person.h"
struct Animal* findAnimalName(struct Animal* head,char* buf)//链表查找
{
struct Animal* pointer=NULL;
pointer=head;
if(pointer == NULL){
printf("not finding......\n");
return NULL;
}else{
while(pointer != NULL){
if(strcmp(pointer->name,buf)==0){
return pointer;
}
pointer=pointer->next;
}
return NULL;
}
}
int main()
{
char buf[128]={'\0'};
struct Animal* head=NULL;
struct Animal* point=NULL;
head=putCatLink(head);//尾插法动态链表
head=putDogLink(head);
head=putPersonLink(head);
while(1){
printf("please inut Animal's name:");
scanf("%s",buf);
point=findAnimalName(head,buf);
if(point != NULL){
point->peat();//打印输出
point->pbeat();
}
memset(buf,'\0',sizeof(buf));//清理空间,初始化
}
return 0;
}