简单工厂模式

1.什么是设计模式

代码设计经验的总结,使代码更加稳定,扩展性更强,这样一系列的编程思想。
设计模式通常描述了一组项目紧密作用的类与对象。(JAVA)
设计模式一共有23种,具体可在这个链接中进行学习:
https://www.runoob.com/design-pattern/design-pattern-tutorial.html
引入设计模式可以使代码更容易被他人理解,保证代码的可靠性,程序的重用性。
C——面向过程 也是一门不太友好的面向对象语言
JAVA——面向对象

2.什么是类和对象

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

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

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

结构体就是类;下面的dog,cat,person就是对象;

#include<stdio.h>

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

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

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

void personEat()
{
	printf("perosn eat everying\n");
}

void personBeat()
{
	printf("person beat\n");
}
int main()
{
	struct Animal dog={
		.peat = dogEat,
	};

	struct Animal cat={
		.peat = catEat,
	};
	struct Animal person={
		.peat = personEat,
		.pbeat = personBeat//赋值后使用(,)逗号,不是用(;)分号
		//最后一项什么都可以不用写,写了逗号也不会报错就是了
		//linux内核源码对结构体就是这样赋值,这样写可以提升逼格,在面试的时候也可能会取得更好的印象
	};

	dog.peat();
	cat.peat();
	person.peat();
	person.pbeat();
	return 0;
}

在这里插入图片描述

3.什么是工厂模式

工厂模式(Factory Pattern)是 常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。

代码示例:
头文件:

#include<stdio.h>

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

	struct Animal *next;

};

struct Animal* putCatInLink(struct Animal *phead);
struct Animal* putdogInLink(struct Animal *phead);
struct Animal* putpersonInLink(struct Animal *phead);

cat.c

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

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

void catBeat()
{
	printf("cat beat\n");
}
struct Animal* putCatInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &cat;
		return phead;
	}else{
		cat.next = phead;
		phead = &cat;
		return phead;
	}
}

struct Animal cat={
	.name = "Cat"
	.peat = catEat,
	.pbeat = catBeat
};

dog.c

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

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

void dogBeat()
{
	printf("dog beat\n");
}
struct Animal* putdogInLink(struct Animal *phead)
{
	if(phead == NULL){
		phead = &dog;
		return phead;
	}else{
		dog.next = phead;
		phead = &dog;
		return phead;
	}
}

struct Animal dog={
	.name = "Dog",
	.peat = catEat,
	.pbeat = catBeat
};

person.c

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

void personEat()
{
        printf("perosn eat everying\n");
}

void personEat()
{
        printf("perosn eat everying\n");
}


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

struct Animal cat={
	.name = "Person"
	.peat = personEat,
	.pbeat = personBeat
};

主函数:

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

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

        if(tmp == NULL){
                printf("without this animal\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 *ptem=NULL;
        phead = putCatInLink(phead);
        phead = putPersonInLink(phead);
        phead = putDogInLink(phead);
        phead = putHouseInLink(phead);
        while(1){
                memset(buf,'\0',sizeof(buf));
                printf("please input animal's name:Cat,Dog,Person\n");
                scanf("%s",buf);
                ptem = findUtilByName(buf,phead);

                if(ptem == NULL)
                {
                        printf("you input name is error\n");
                        printf("please input again\n");
                }
                if(ptem != NULL){                        
                		ptem->peat();
                        ptem->pbeat();
                }
        }

}

结果示例:

在这里插入图片描述

使用工厂模式可以使我们更加方便的添加我们所需的功能,比如我们需要添加一个house.c进去,我们只需编写house.c的文件,并且添加函数到头文件即可

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

void houseEat()
{
        printf("house eat grass\n");
}

void houseBeat()
{
        printf("house beat\n");
}

struct Animal house={
        .name = "House",
        .peat = houseEat,
        .pbeat = houseBeat
};
struct Animal* putHouseInLink(struct Animal *phead)
{
        if(phead == NULL){
                phead = &house;
                return phead;
        }else{
                house.next = phead;
                phead = &house;
                return phead;
        }
}

在这里插入图片描述

在这里插入图片描述

这样house就被添加进去了
在这里插入图片描述
这边博文写的更加详细感兴趣的小伙伴可以去参考————设计模式——简易工厂模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值