一、搭建模型
二、设计模式的概念引入
1.什么是设计模式
软件设计模式(Design pattern),又称设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性、程序的重用性。
设计模式(英语 design pattern)是对面向对象设计中反复出现的问题的解决方案。这个术语是在1990年代由Erich Gamma等人从建筑设计领域引入到计算机科学中来的。这个术语的含义还存有争议。算法不是设计模式,因为算法致力于解决问题而非设计问题。设计模式通常描述了一组相互紧密作用的类与对象。设计模式提供一种讨论软件设计的公共语言,使得熟练设计者的设计经验可以被初学者和其他设计者掌握。设计模式还为软件重构提供了目标。
设计模式通常描述了一组相互紧密作用的类与对象。(java)
c:面向过程,一种不太友好的面向对象语言
java:面向对象
建筑设计领域引入到计算机科学中来的
23种 代码更容易被他人理解、保证代码可靠性、程序的重用性
参考文章:https://www.runoob.com/design-pattern/factory-pattern.html
2.什么是类和对象
类:是一种用户定义的引用数据类型,也称类类型,结构体
对象:类的一种具象
#include <stdio.h>
//类:抽象
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"
#include <string.h>
struct Animal *findAnimal(char *buf,struct Animal *head)
{
struct Animal *p = head;
if(p == NULL)
{
printf("none\n");
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] = {'\0'};
head = putCatInLine(head);
head = putDogInLine(head);
head = putPersonInLine(head);
while(1)
{
printf("please input which one you want to find\n");
scanf("%s",buf);
find = findAnimal(buf,head);
if(find == NULL)
{
printf("not found\n");
}else
{
find->peat();
find->pbeat();
}
memset(buf,'\0',sizeof(buf));
}
return 0;
}
Animal.h
#include <stdio.h>
struct 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 fish\n");
}
void catBeat()
{
printf("cat beat mouse\n");
}
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 shi\n");
}
void dogBeat()
{
printf("dog beaat cat\n");
}
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 meat\n");
}
void personBeat()
{
printf("person beaat eachother\n");
}
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;
}
}