智能家居代码架构---简单工厂模式

该文章介绍了在智能家居项目中如何使用人脸识别技术,涉及祥云平台编程及编译libcurl库以支持SSL,同时安装了SSL依赖库libssl和libcrypto。文章还探讨了编程思想,强调稳定性和拓展性,并讲解了设计模式,特别是工厂模式的应用,以创建和管理不同类型的对象,便于代码理解和维护。
摘要由CSDN通过智能技术生成

(11条消息) 智能家居 (10) ——人脸识别祥云平台编程使用(编译libcurl库支持SSL,安装SSL依赖库libssl、libcrypto)openssl 依赖库行稳方能走远的博客-CSDN博客

看上面这个博客的往期文章

代码设计经验的总结,稳定,拓展性更强。一系列编程思想 算法不是设计模式,因为算法致力于解决问题而非设计问题。

ftp get put cd lcd pwd lls ls.... 乱

什么是设计模式

设计模式通常描述了一组相互紧密作用的类与对象。(java) c 面向过程,一门不太友好的面向对象语言 java 面向对象

建筑设计领域引入到计算机科学中来的

23种 代码更容易被他人理解、保证代码可靠性、程序的重用性。 设计模式 | 菜鸟教程 (runoob.com) 百科 软件设计模式_百度百科 (baidu.com)

什么是类和对象

类是一种用户定义的引用数据类型,也称类类型。结构体 对象: 类的一种具象

struct Animal{ int age; int sex; //成员属性 void *peat(); void *pbeat(); //成员方法 }

struct Animal dog; struct Animal cat; struct Animal person;

什么是工厂模式

工厂模式(Factory Pattern)是最常用的设计模式之一。 这种类型的设计模式属于创建型模式,它提供了一种创建对象的 (最 佳)方式。

在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

代码如下:

#include <stdio.h>


struct Animal{
        char name[20];
        int sex;	//成员属性
        void (*peat)();//成员方法

};

void dogEat(void)
{
        printf("dog eats shit\n");
}
void catEat(void)
{
        printf("cat eats fish\n");
}
int main()
{
        struct Animal cat;
        struct Animal dog;

        cat.peat = catEat;
        dog.peat = dogEat;

        dog.peat();
        cat.peat();

        return 0;
}

更好的写法:

#include <stdio.h>


struct Animal{
        char name[20];
        int sex;
        void (*peat)();

};

void dogEat(void)
{
        printf("dog eats shit\n");
}
void catEat(void)
{
        printf("cat eats fish\n");
}
int main()
{
        struct Animal cat = {
                            .peat = catEat//选择性赋值
                                };
        struct Animal dog = {
                            .peat = dogEat
                                };

        dog.peat();
        cat.peat();

        return 0;
}

工厂模式的实现

animal.h

#include <stdio.h>


struct Animal{
		char name[128];
		int  age;
		int  sex;
		int  others;
		void (*peat)();
		void (*pbeat)();
		void (*test)();

		struct Animal *next;

};

struct Animal* putPersonInLink(struct Animal *phead);//先声明
struct Animal* putCatInLink(struct Animal *phead);
struct Animal* putDogInLink(struct Animal *phead);

mainPro.c

#include "animal.h"
#include <string.h>

struct Animal* findUtilByName(char* str, struct Animal* phead)//结构体查询
{
	struct Animal *tmp= phead;

	if(phead== NULL){
		printf("空\n");
		return NULL;
	}else{
		while(tmp != NULL){
			if(strcmp(tmp->name,str) == 0){
				return tmp;
			}
			tmp=tmp->next;
		}
		return NULL;
	}
}

int main()
{
	char buf[128]={'\0'};
	struct Animal *phead = NULL;
	struct Animal *ptmp;

	phead = putCatInLink(phead);
	phead = putDogInLink(phead);
	phead = putPersonInLink(phead);//至此已经组合完成整个链表

	
	while(1){
		printf("please input:Tom,ahuang, xiaoming\n");  //链表的查询
		scanf("%s",buf);
		ptmp = findUtilByName(buf,phead);

		if(ptmp != NULL){
		ptmp->pbeat();
		ptmp->peat();
		}
		memset(buf, '\0',sizeof(buf)); //此处不能用strlen
	}

	return 0;
}

cat.c

#include "animal.h"

void catEat()
{
	printf("cat eat fish\n");
}

void catBeat()
{
	printf("cat bite your little brother\n");
}

struct Animal cat = {
	.name = "Tom",
	.peat = catEat,
	.pbeat = catBeat

};


struct Animal* putCatInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &cat;
		return phead;
	}else{
		cat.next = phead;  //头插法
		phead = &cat;
		return phead;     //返回最新的头
		
	}
}

dog.c

#include "animal.h"

void dogEat()
{
	printf("dog eat shi\n");
}

void dogBeat()
{
	printf("dog bite your little brother\n");
}

struct Animal dog = {
	.name = "ahuang",
	.peat = dogEat,
	.pbeat = dogBeat

};


struct Animal* putDogInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &dog;
		return phead;
	}else{
		dog.next = phead;
		phead = &dog;
		return phead;
		
	}
}

person.c

#include "animal.h"

void personEat()
{
	printf("person eat rice\n");
}

void personBeat()
{
	printf("person bite  your brother\n");
}
  
struct Animal person = {
	.name = "xiaoming",
	.peat = personEat,
	.pbeat = personBeat

};


struct Animal* putPersonInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &person;
		return phead;
	}else{
		person.next = phead;
		phead = &person;
		return phead;
		
	}
}

编译运行

如若想在链表上加入其它的animal,直接新建编写一个.C文件,然后加入到链表中。

这样就完成类似不断给智能家居加入新功能,同时也有助于代码的维护。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aurora Smith

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值