智能家居-基于香橙派zreo2——手把手搭建、继电器控制灯(一)

一、什么是设计模式

        设计模式(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;
	}
};

准备就绪

编译运行

 运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#ifndef Node_hpp #define Node_hpp #include <stdio.h> class Node { public: virtual double Calc() const =0; virtual ~Node(){}; }; class NumberNode:public Node { public: double Calc() const; NumberNode(double number):number_(number){}; private: const double number_; }; class BinaryNode:public Node { public: BinaryNode(Node* left,Node* right):left_(left),right_(right){} ~BinaryNode(); protected: Node* const left_; Node* const right_; }; class UnaryNode:public Node { public: double Calc() const; UnaryNode(Node* child):child_(child){} ~UnaryNode(); protected: Node* const child_; }; class AddNode:public BinaryNode { public: AddNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class SubNode:public BinaryNode { public: SubNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class MultiplyNode:public BinaryNode { public: MultiplyNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class DivideNode:public BinaryNode { public: DivideNode(Node* left,Node* right):BinaryNode(left,right){} double Calc() const; }; class UMinusNode:public UnaryNode { public: UMinusNode(Node* child):UnaryNode(child){} double Calc() const; }; #endif#include "Node.hpp" #include <iostream> using namespace std; #include <cmath> double NumberNode:: Calc() const{ return number_; } BinaryNode::~BinaryNode(){ delete left_; delete right_; } UnaryNode::~UnaryNode(){ delete child_; } double AddNode:: Calc() const{ return left_->Calc()+right_->Calc(); } double SubNode:: Calc() const{ return left_->Calc()-right_->Calc(); } double MultiplyNode::Calc() const{ return left_->Calc()*right_->Calc(); } double DivideNode:: Calc() const{ double divisor=right_->Calc(); if (divisor!=0.0) { return left_->Calc()/divisor; } else { cout<<"Error:divisor by zreo"<<endl; return HUGE_VAL; } return left_->Calc()+right_->Calc(); } double UMinusNode::Calc() const{ return - child_->Calc(); }
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值