智能家居 (2) ——设计模式的引入
目錄
- 設(shè)計(jì)模式的概念引入
- 工廠模式的實(shí)現(xiàn)
- animal.h
- mainPro.c
- cat.c
- dog.c
- person.c
- 工廠模式的功能驗(yàn)證
- 往期文章
設(shè)計(jì)模式的概念引入
工廠模式的實(shí)現(xiàn)
所有代碼最好在Source Insight下編寫,并將所有代碼進(jìn)行關(guān)聯(lián),方便讀寫。
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)//結(jié)構(gòu)體查詢 {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);//至此已經(jīng)組合完成整個鏈表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;} }工廠模式的功能驗(yàn)證
將上述代碼放到ubuntu下運(yùn)行,效果如下:
用Source Insight編寫代碼會有一些編碼格式問題,拿過來放到ubuntu上運(yùn)行會出現(xiàn)亂碼,最好不要有中文。
如若想在鏈表上加入其它的animal,直接新建編寫一個.C文件,然后加入到鏈表中。
這樣就完成類似不斷給智能家居加入新功能,同時也有助于代碼的維護(hù)。
往期文章
智能家居 (1) ——智能家居整體功能框架
智能家居 (2) ——設(shè)計(jì)模式的引入
智能家居 (3) ——工廠模式繼電器控制燈
智能家居 (4) ——工廠模式火焰報(bào)警
智能家居 (5) —— LD3320語音模塊二次開發(fā)
智能家居 (6) ——語音識別線程控制
智能家居 (7) ——網(wǎng)絡(luò)服務(wù)器線程控制
智能家居 (8) ——智能家居項(xiàng)目整合(網(wǎng)絡(luò)控制線程、語音控制線程,火災(zāi)報(bào)警線程)
網(wǎng)絡(luò)編程知識預(yù)備(1) ——了解OSI網(wǎng)絡(luò)模型
網(wǎng)絡(luò)編程知識預(yù)備(2) ——淺顯易懂的三次握手與四次揮手
網(wǎng)絡(luò)編程知識預(yù)備(3) ——SOCKET、TCP、HTTP之間的區(qū)別與聯(lián)系
網(wǎng)絡(luò)編程知識預(yù)備(4) ——了解HTTP協(xié)議與HTTPS協(xié)議
網(wǎng)絡(luò)編程知識預(yù)備(5) ——libcurl庫簡介及其編程訪問百度首頁
智能家居 (9) ——人臉識別攝像頭安裝實(shí)現(xiàn)監(jiān)控功能
智能家居 (10) ——人臉識別祥云平臺編程使用
智能家居 (11) ——樹莓派攝像頭捕捉人臉并識別
智能家居 (12) ——人臉識別整合到智能家居系統(tǒng)
智能家居 (13) ——智能家居加入手機(jī)app端控制
總結(jié)
以上是生活随笔為你收集整理的智能家居 (2) ——设计模式的引入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vector 特性
- 下一篇: asp.net 网站开发流程总结