智能家居项目开发(一):简单工厂设计模式引入

研究院院长 LV8
2023-05-31 · 63 阅读
一、智能家居功能细节拆分

1.控制区
我们会用到之前学过的:
语音识别模块
socket客户端

2.外设区
继电器组控制灯,远程终端子系统控制灯,窗帘等,火灾报警器,摄像头。

二、设计模式概念的引入
项目开发我们会用到简单的工厂设计模式,所以接下来我们会了解到设计模式。

1. 什么是设计模式?
设计模式(英语 design pattern)是对面向对象设计中反复出现的问题的解决方案。
这个术语是在1990年代由Erich Gamma等人从建筑设计领域引入到计算机科学中来的。
这个术语的含义还存有争议。算法不是设计模式,因为算法致力于解决问题而非设计问题。
设计模式通常描述了一组相互紧密作用的类与对象。设计模式提供一种讨论软件设计的公共语言,使得熟练设计者的设计经验可以被初学者和其他设计者掌握。设计模式还为软件重构提供了目标。

2.为什么要用到设计模式?
因为这是大佬们多年代码设计经验的总结,从而创造出了代码设计模式,它是一系列编程思想。
我们可以利用这种思想编写代码,从而让我们的代码更加的稳定,拓展性更强。

3.设计模式有23种:
设计模式参考博文

它会使得代码更容易被他人理解、保证代码可靠性、程序的稳定性。

三、类与对象
上面讲到设计模式通常描述一组互相紧密作用的类与对象。

1.什么是类和对象?
类:是一种用户定义的引用数据类型,也称类类型。
C语言中结构体就属于这样一种数据类型。
比如:

struct Animal   //一种类别
{
   char name[128];
   int age;
   int six;   //成员属性
   void (*pead)();
   void (*pbeat)();   //成员方法

};


对象:类的一种具象。

struct Animal dog;
struct Animal cat;
struct Animal person;   //类的一种具体对象



2.类与对象的拓展

#include<stdio.h>
//类 : 抽象   模板
struct Animal
{
       char name[128];
       int age;
       int six;
       int others;
       void(*peat)();
       void(*pbeat)();
       void(*text)();
};

voiddogEat()
{
     printf("狗吃屎\n");
}

voidcatEat()
{
     printf("猫吃鱼\n");
}

voidpersonEat()
{
     printf("人吃米\n");
}

voiddogBeat()
{
     printf("咬你小弟弟\n");
}

voidcatBeat()
{
     printf("扣你小弟弟\n");
}

voidpersonBeat()
{
     printf("猴子偷桃\n");
}
int main()
{

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

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

    dog.pbeat = dogBeat;
    cat.pbeat = catBeat;
    person.pbeat = personBeat;

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

    dog.pbeat();
    cat.pbeat();
    person.pbeat();

}




运行结果:


四、结构体新玩法
1.我们回顾下之前学的结构体:

#include<stdio.h>
#include<string.h>
struct Animal
{
       char name[128];
       int age;
       int six;
       int other;
       void (*peat)();
       void (*pbeat)();

};

voiddogEat()
{
     printf("狗吃屎\n");
}

voiddogBeat()
{
     printf("咬你小弟弟\n");
}

int main()
{
    //我们之前是这样挨个给结构体中的元素复赋值
    struct Animal dog = {"阿黄",1,1,100,dogEat,dogBeat};

    printf("name = %s\n",dog.name);
    printf("age = %d\n",dog.age);
    printf("six = %d\n",dog.six);
    printf("other = %d\n",dog.other);
    dog.peat();
    dog.pbeat();

    return0;
}



运行结果:

2.现在我们这样玩结构体:
由于某些业务需求不完全需要结构体中的元素,我们只选取需要用到的元素。

#include<stdio.h>

struct Animal
{
       char name[128];
       int age;
       int six;
       int other;
       void (*peat)();
       void (*pbeat)();

};

voiddogEat()
{
     printf("狗吃屎\n");
}

voiddogBeat()
{
     printf("咬你小弟弟\n");
}

int main()
{

    struct Animal dog = {
           .peat = dogEat,
           .pbeat = dogBeat
    };

    dog.peat();
    dog.pbeat();

    return0;
}





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

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

2.工厂模式的实现:
我们是采用分文件编程的方式实现。

第一步: 先创建文件factory,然后在文件内创建主函数文件和其他业务文件,也就是把上面的主函数main.c拆分成多个文件
第二步: 每个文件建议用Sourse Insight,这样更加直观和方便

主函数main.c代码:
它是先把其他文件通过链表链起来,然后让数据产生联系,这样我们就可以具体到某个业务

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

struct Animal *findUtilByName(char *str,struct Animal *phead)
{
       struct Animal *tmp = phead;

       if(phead == NULL){
          printf("kong\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("input:tom ,teddy ,handsome\n");
            scanf("%s",buf);
            ptmp =findUtilByName(buf,phead);

            if(ptmp != NULL){
               ptmp->peat();
               ptmp->pbeat();

            }
            memset(buf,'\0',sizeof(buf));
    }

    return0;
}




cat.c代码:

#include"Animal.h"

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

voidcatBeat()
{
      printf("cat scratches man\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"

voiddogEat()
{
      printf("dog eat shit\n");
}

voiddogBeat()
{
      printf("dog beats people\n");
}

struct Animal dog = {
       .name ="teddy",
       .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"

voidpersonEat()
{
      printf("Human eating rice\n");
}

voidpersonBeat()
{
      printf("Beat a beast\n");
}

struct Animal person = {
       .name ="handsome",
       .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.h代码:
注意: 每个文件都包含了这个文件名字,相当于.h头文件。要用到该文件就要在代码里的头文件加上include"Animal.h" 其中 " " 是指在该路径下去寻找Animal.h 的文件,没找到就默认去include 底下去找,没找到就报错

#include<stdio.h>

struct Animal{

       char name[128];
       int age;
       int six;
       int other;
       void(*peat)();
       void(*pbeat)();
       void(*text)();

       struct Animal *next;
    };

struct Animal *putCatInLink(struct Animal * phead);
struct Animal *putDogInLink(struct Animal * phead);
struct Animal *putPersonInLink(struct Animal *phead);
//函数声明,以便主函数调用



写好之后,将factory文件拖进ubuntu下进行编译运行:
所以利用这种简单的工厂模式,以后想加一个业务模块只要创建一个文件,不会破坏到其他文件。



1. 本站所有资源来源于用户上传和网络,仅作为演示数据,如有侵权请邮件联系站长!
2. 盗版,破解有损他人权益和违法作为,请各位站长支持正版!
回复

举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则