智能家居(1) -- 基础概念引入(设计、工厂模式)

文章介绍了设计模式中的工厂模式,通过C语言展示了如何使用结构体模拟类和对象,以及如何实现工厂模式来创建和管理这些对象。工厂模式提供了一种在代码中创建对象而不暴露创建逻辑的方式,增强了代码的可维护性和扩展性。
摘要由CSDN通过智能技术生成

目录

概念引入

一、设计模式

1、什么是设计模式(面试考点)

2、什么是类和对象

 举例:用C语言做一次类和对象

二、C语言中结构体的新玩法,承接上一步

三、工厂模式概念引入

1、什么是工厂模式

四、工厂模式的实现

animal.h

mainPro.c

cat.c

dog.c

person.c

工厂模式的功能验证


概念引入

一、设计模式

1、什么是设计模式(面试考点)

设计模式有23种,我用过工厂模式,之所以我学设计模式的原因是,以前我写代码比较乱,在添加功能的时候,会导致其他功能的代码被破坏,所以我去学的设计模式

2、什么是类和对象

类:是一种用户定义的引用数据类型,在C语言中也称之为结构体

struct Animal{--------->类
 
    int age; ---------->成员的属性
    int sex;
    void *peat();------>成员的方法
 
};

对象:类的一种具体对象

struct Animal dog;
struct Animai cat;

 举例:用C语言做一次类和对象

#include <stdio.h>
 
struct Animal{          //类
        char name[32];
        int eat;
        int age;
        int sex;
        void (*peat)();
};
void dogEat(){
        printf("dog chi shi\n");
}
void catEat(){
        printf("cat chi yu\n");
}
void personEat(){
        printf("person chi mi\n");
}
 
int main(){
 
        struct Animal dog;
        struct Animal cat;
        struct Animal person;   //对象,事务的具象
 
        dog.peat = dogEat;
        cat.peat = catEat;
        person.peat = personEat;
 
        dog.peat();
        cat.peat();
        person.peat();
 
        return 0;
}

二、C语言中结构体的新玩法,承接上一步

#include <stdio.h>
 
struct Animal{          //类
        char name[32];
        int eat;
        int age;
        int sex;
        void (*peat)();
};
void dogEat(){
        printf("dog chi shi\n");
}
void catEat(){
        printf("cat chi yu\n");
}
void personEat(){
        printf("person chi mi\n");
}
 
int main(){
 
        struct Animal dog = {   //对象,事务的具象
                .name = "a huang",
                .peat = dogEat
        };
        struct Animal cat = {
                .name = "a mao",
                .peat = catEat
        };
        struct Animal person = {
                .name = "a ren",
                .peat = personEat
        };
 
        dog.peat();
        cat.peat();
        person.peat();
        printf("%s\n",dog.name);
 
        return 0;
}

三、工厂模式概念引入

1、什么是工厂模式

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

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

如上图中,左侧可以比拟为一个工厂,工厂里有很多个可能,右侧可比拟一个main函数的入口,当我们用到哪个功能时, 就在main函数中调用工厂里的功能接口(API)就可以了,如果想要在工厂里建立一个新功能时,直接新添加一个文件就好,工厂中一个独立的文件就代表是一个独立的功能,各个功能之间互不干扰,比如你改错了一个功能文件,但不影响其他的功能文件

四、工厂模式的实现

在这里插入图片描述

所有代码最好在Source Insight下编写,并将所有代码进行关联,方便读写。

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;
		
	}
}

工厂模式的功能验证

将上述代码放到ubuntu下运行,效果如下:
在这里插入图片描述

用Source Insight编写代码会有一些编码格式问题,拿过来放到ubuntu上运行会出现乱码,最好不要有中文。

如若想在链表上加入其它的animal,直接新建编写一个.C文件,然后加入到链表中。
这样就完成类似不断给智能家居加入新功能,同时也有助于代码的维护。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值