树莓派——linux设计模式(工厂模式)

本文介绍了设计模式的概念,源自建筑设计领域,旨在提升代码的可读性和可靠性。重点讲解了工厂模式,它是创建型模式中最常用的一种,用于隐藏对象创建的复杂性并提供一个统一的接口。在树莓派上,通过实例展示了如何在C语言中实现工厂模式,创建cat、dog、person等对象,强调了该模式在提高代码拓展性方面的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 代码设计经验的总结。让代码更加稳定,拓展性更强,一系列的编程思想。

1、什么是设计模式

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

        一共有23种设计模式。让代码更容易被他人理解、保证代码可靠性、程序的重要性。

        c语言:面向过程,一门不太友好的面向对象的语言

        java:面向对象 

2、什么是类和对象

        类是一种用户定义的引用数据类型,也称类类型。c语言中的结构体;

         对象:类的一种具象


#include <stdio.h>
//类,抽象 模板
struct animal{
	char name[128];
	int age;
	int sex;
    int other;
	void (*peat)();
	void (*pbeat)();
    char test[32];
};

void dogeat()
{
printf("this is dog eat\n");
}
void cateat()
{
	printf("this is cat eat\n");
}
void personeat()
{
	printf("this is person eat\n");
}

void dogbeat()
{
	printf("yaoni\n");
}
void catbeat()
{
	printf("zhuani\n");
}
void personbeat()
{
	printf("kouni\n");
}
int main()
{
    struct animal dog={"阿黄",1,1,100,dogeat,dogbeat,"dd"};//直接赋值
/*    struct animal dog2={
        .peat=dogeat,
        .pbeat=dogbeat;
    };
*/
	struct animal cat;//对象,事务的具象
//	struct animal dog;
	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();
	return 0;
}

cat、dog、person都是类animal的一种对象

结果:

yzr@ubuntu:~/SYSTEM$ vi oop1.c 
yzr@ubuntu:~/SYSTEM$ gcc oop1.c 
yzr@ubuntu:~/SYSTEM$ ./a.out
this is dog eat
this is cat eat
this is person eat
yaoni
zhuani
kouni

3、什么是工厂模式 

         工厂模式是最常用的设计模式之一。

        这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

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

工厂中创建对象函数变量,以链表的形式串起来。

函数api暴露出来给main函数使用

4、编程实现 

cat.c

#include "animal.h"

void cateat()
{
	printf("this is cat eat\n");
}

void catbeat()
{
	printf("zhuani\n");
}


struct animal cat={
	.name="tom",
	.peat=cateat,
	.beat=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("this is dog eat\n");
}

void dogbeat()
{
	printf("yaoni\n");
}


struct animal dog={
	.name="huang",
	.peat=dogeat,
	.beat=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("this is person eat\n");
}

void personbeat()
{
	printf("kouni\n");
}


struct animal person={
	.name="zhang",
	.peat=personeat,
	.beat=personbeat
};

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

animal.h

#include <stdio.h>

struct animal{
	char name[128];
	int age;
	int sex;
    int other;
	void (*peat)();
	void (*beat)();
    char test[32];

	struct animal *next;
};

struct animal *putCatInLink(struct animal *phead);
struct animal *putDogInLink(struct animal *phead);
struct animal *putPersonInLink(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;//找不到遍历链表
		}
	}
}

int main()
{
	char buf[128]={'\0'};
	struct animal *ptmp;
	struct animal *phead=NULL;
	
	phead=putCatInLink(phead);//添加进链表
	phead=putDogInLink(phead);
	phead=putPersonInLink(phead);

	while(1){
		printf("请输入:tom、huang、zhang\n");
		scanf("%s",buf);
		ptmp =findUtilByName(buf,phead);//遍历链表查找
		if(ptmp!=NULL){
			ptmp->beat();
			ptmp->peat();
		}
		memset(buf,'\0',sizeof(buf));
	}
	return 0;
}

添加文件:project→newproject→ 修改文件路径,选择相关文件夹(一般在项目下建立si文件),建立完成然后叉掉

 ctrl+o右侧显示项目列表,view中的line number可以打开行号。

编译运行: 

yzr@ubuntu:~/Desktop/factory$ gcc *.c -o fac
yzr@ubuntu:~/Desktop/factory$ ./fac
请输入:tom、huang、zhang
tom
zhuani
this is cat eat
请输入:tom、huang、zhang
huang
yaoni
this is dog eat
请输入:tom、huang、zhang
zhang
kouni
this is person eat
请输入:tom、huang、zhang

可以看出,工厂模式的可拓展性很强。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值