1.什么是设计模式
软件设计模式(Design pattern),又称设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。
设计模式(英语 design pattern)是对面向对象设计中反复出现的问题的解决方案。这个术语是在1990年代由Erich Gamma等人从建筑设计领域引入到计算机科学中来的。这个术语的含义还存有争议。算法不是设计模式,因为算法致力于解决问题而非设计问题。设计模式通常描述了一组相互紧密作用的类与对象。设计模式提供一种讨论软件设计的公共语言,使得熟练设计者的设计经验可以被初学者和其他设计者掌握。设计模式还为软件重构提供了目标。
设计模式通常描述了一组相互紧密作用的类与对象。(java)
c:面向过程,一种不太友好的面向对象语言
java:面向对象
建筑设计领域引入到计算机科学中来的
23种 代码更容易被他人理解、保证代码可靠性、程序的重用性
参考文章:https://www.runoob.com/design-pattern/factory-pattern.html
2.什么是类和对象
类:是一种用户定义的引用数据类型,也称类类型,结构体
对象:类的一种具象
#include三、工厂模式概念引入//类:抽象 struct Animal { int age; //成员属性 char name[128]; void (*peat)(); //成员方法 void (*pbeat)(); }; void catEat() { printf("猫吃鱼n"); } void dogEat() { printf("狗吃屎n"); } void personEat() { printf("人吃肉n"); } int main() { struct Animal dog; struct Animal cat; struct Animal person; dog.peat = dogEat; cat.peat = catEat; person.peat = personEat; dog.peat(); cat.peat(); person.peat(); return 0; }
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
for example
mainpro.c
#include "Animal.h" #includestruct Animal *findAnimal(char *buf,struct Animal *head) { struct Animal *p = head; if(p == NULL) { printf("nonen"); return NULL; }else { while(p != NULL) { if(strcmp(buf,p->name) == 0) { return p; } p = p->next; } return NULL; } } int main() { struct Animal *head = NULL; struct Animal *find = NULL; char buf[128] = {' '}; head = putCatInLine(head); head = putDogInLine(head); head = putPersonInLine(head); while(1) { printf("please input which one you want to findn"); scanf("%s",buf); find = findAnimal(buf,head); if(find == NULL) { printf("not foundn"); }else { find->peat(); find->pbeat(); } memset(buf,' ',sizeof(buf)); } return 0; }
Animal.h
#includestruct Animal { char name[128]; int age; void (*peat)(); void (*pbeat)(); struct Animal *next; }; struct Animal *putCatInLine(struct Animal *head); struct Animal *putDogInLine(struct Animal *head); struct Animal *putPersonInLine(struct Animal *head);
cat.c
#include "Animal.h"
void catEat()
{
printf("cat eat fishn");
}
void catBeat()
{
printf("cat beat mousen");
}
struct Animal cat = {
.name = "Tom",
.peat = catEat,
.pbeat = catBeat
};
struct Animal *putCatInLine(struct Animal *head)
{
if(head == NULL)
{
head = &cat;
}else
{
cat.next = head;
head = &cat;
}
return head;
}
dog.c
#include "Animal.h"
void dogEat()
{
printf("dog eat shin");
}
void dogBeat()
{
printf("dog beaat catn");
}
struct Animal dog = {
.name = "wangwang",
.peat = dogEat,
.pbeat = dogBeat
};
struct Animal *putDogInLine(struct Animal *head)
{
if(head == NULL)
{
head = &dog;
}else
{
dog.next = head;
head = &dog;
}
return head;
}
person.c
#include "Animal.h"
void personEat()
{
printf("person eat meatn");
}
void personBeat()
{
printf("person beaat eachothern");
}
struct Animal person = {
.name = "wangsicong",
.peat = personEat,
.pbeat = personBeat
};
struct Animal *putPersonInLine(struct Animal *head)
{
if(head == NULL)
{
head = &person;
}else
{
person.next = head;
head = &person;
}
return head;
}
同步文件
编译:
gcc *.c四、智能家居项目框架设计
一个main函数,两个工厂
指令工厂(语音,socket客户端 。两个组成链表)
控制工厂(各种灯,火灾报警,锁,wemosD1串口交互。组成链表)、、摄像头
contrlDevices.h
struct Devices
{
char deviceName[128];
int status;
int (*open)();
int (*close)();
int (*deviceInit)();
int (*readStatus)();
int (*changeStatus)(int status);
struct Devices *next;
};
InputCommand.h
struct InputCommander
{
char commandName[128];
char command[32];
int (*Init)(char *name,char *ipAdress,char *port);
int (*getCommand)(char *cmd);
char log[1024];
struct InputCommander *next;
};
bathroomLight.c
#include "contrlDevices.h"
int bathroomLightOpen()
{
}
int bathroomLightClose()
{
}
int bathroomLightInit()
{
}
int bathroomLightStatus()
{
}
struct Devices bathroomLight = {
.deviceName = "bathroomLight",
.open = bathroomLightOpen,
.close = bathroomLightClose,
.deviceInit = bathroomLightInit,
.changeStatus = bathroomLightStatus
};
struct Devices *addBathroomLightTolink(struct Devices *phead)
{
if(phead == NULL)
{
return &bathroomLight;
}else
{
bathroomLight.next = phead;
phead = &bathroomLight;
return phead;
}
}



