一、什么是设计模式
设计模式(Design pattern)代表了最佳的实践,通常被有经验的面向对象的软件开发人员所采用。设计模式是软件开发人员在软件开发过程中面临的一般问题的解决方案。这些解决方案是众多软件开发人员经过相当长的一段时间的试验和错误总结出来的
设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结。
使用设计模式是为了重用代码、让代码更容易被他人理解、保证代码可靠性。
二、类和对象
类是面向对象程序设计实现信息封装的基础。类是一种用户定义的引用数据类型(结构体),也称类类型。每个类包含数据说明和一组操作数据或传递消息的函数。类的实例称为对象。
struct Animal{
int age;
int sex; //成员属性
void *peat();
void *pbeat();//成员方法
}
对象:类的实例称为对象
struct Animal dog;
struct Animal cat;
三、工厂模式
工厂模式(Factory Pattern)是 最常用的设计模式之一。
这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
工厂模式的实现
————————————————
版权声明:本文为CSDN博主「大头1213」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Zy_1213/article/details/126679238
四、工厂模式实现继电器控制所有灯(自己的代码)
新建文件夹取名smartHouse,新建文件如下
在Source Insight里建立项目
主函数Mainpro.c
#include <stdio.h>
#include <string.h>
#include "controlDevice.h"
struct Device *findDeviceByName(char *name,struct Device *phead)
{
struct Device* tmp = phead;
if(phead == NULL){
return NULL;
}else{
while(tmp != NULL){
if(strcmp(tmp->DeviceName,name) == 0){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
}
int main()
{
if(wiringPiSetup() == -1){
printf("set up error!\n");
return -1;
}
char *name = "restaurant";
struct Device *pDeviceHead = NULL;
pDeviceHead = addrBathroomLight(pDeviceHead);
pDeviceHead = addrlivingRoomLight(pDeviceHead);
pDeviceHead = addrrestaurantLight(pDeviceHead);
pDeviceHead = addrsecondfloorLight(pDeviceHead);
struct Device *tmp = NULL;
char nn[128];
while(1){
printf("Input:\n");
scanf("%s",nn);
tmp = findDeviceByName(nn,pDeviceHead);
if(tmp != NULL){
tmp->DeviceInit(tmp->pinNum);
tmp->open(tmp->pinNum);
}
}
return 0;
}
设备管理controlDevice.h
#include <wiringPi.h>
#include <stdlib.h>
struct Device
{
char DeviceName[128];
int status;
int pinNum;
int (*open)(int pinNum);
int (*close)(int pinNum);
int (*DeviceInit)(int pinNum);
int (*readStatus)();
int (*changeStatus)();
struct Device *next;
};
struct Device *addrBathroomLight(struct Device *phead);
struct Device *addrlivingRoomLight(struct Device *phead);
struct Device *addrrestaurantLight(struct Device *phead);
struct Device *addrsecondfloorLight(struct Device *phead);
浴室灯Light-bathroom.c
#include "controlDevice.h"
#include <stdlib.h>
int BathroomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int BathroomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int BathroomLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int BathroomLightSataus(int status)
{
}
struct Device BathroomLight ={
.DeviceName = "Bathroom",
.pinNum = 5,
.open = BathroomLightOpen,
.close = BathroomLightClose,
.DeviceInit = BathroomLightInit,
.changeStatus = BathroomLightSataus
};
struct Device *addrBathroomLight(struct Device *phead)
{
if(phead == NULL){
return &BathroomLight;
}else{
BathroomLight.next = phead;
phead = &BathroomLight;
return phead;
}
};
客厅灯Light-livingroom
#include "controlDevice.h"
#include <stdlib.h>
int livingRoomLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int livingRoomLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int livingRoomLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int livingRoomLightSataus(int status)
{
}
struct Device livingRoomLight ={
.DeviceName = "livingRoom",
.pinNum = 2,
.open = livingRoomLightOpen,
.close = livingRoomLightClose,
.DeviceInit = livingRoomLightInit,
.changeStatus = livingRoomLightSataus
};
struct Device *addrlivingRoomLight(struct Device *phead)
{
if(phead == NULL){
return &livingRoomLight;
}else{
livingRoomLight.next = phead;
phead = &livingRoomLight;
return phead;
}
};
餐厅灯Light-restaurant.c
#include "controlDevice.h"
#include <stdlib.h>
int restaurantLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int restaurantLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int restaurantLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int restaurantLightSataus(int status)
{
}
struct Device restaurantLight ={
.DeviceName = "restaurant",
.pinNum = 7,
.open = restaurantLightOpen,
.close = restaurantLightClose,
.DeviceInit = restaurantLightInit,
.changeStatus = restaurantLightSataus
};
struct Device *addrrestaurantLight(struct Device *phead)
{
if(phead == NULL){
return &restaurantLight;
}else{
restaurantLight.next = phead;
phead = &restaurantLight;
return phead;
}
};
二楼灯Light-secondfloor.c
#include "controlDevice.h"
#include <stdlib.h>
int secondfloorLightOpen(int pinNum)
{
digitalWrite(pinNum,LOW);
}
int secondfloorLightClose(int pinNum)
{
digitalWrite(pinNum,HIGH);
}
int secondfloorLightInit(int pinNum)
{
pinMode(pinNum,OUTPUT);
digitalWrite(pinNum,HIGH);
}
int secondfloorLightSataus(int status)
{
}
struct Device secondfloorLight ={
.DeviceName = "secondfloor",
.pinNum = 8,
.open = secondfloorLightOpen,
.close = secondfloorLightClose,
.DeviceInit = secondfloorLightInit,
.changeStatus = secondfloorLightSataus
};
struct Device *addrsecondfloorLight(struct Device *phead)
{
if(phead == NULL){
return &secondfloorLight;
}else{
secondfloorLight.next = phead;
phead = &secondfloorLight;
return phead;
}
};