学习笔记——工厂模式

一.工厂模式定义

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


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

二.简单工厂模式练习

在工厂模式中,把所有的功能代码都写进我们的“工厂”中,再由main函数调用。

animal.h 

#include <stdio.h>

struct Animal {
	char name[128];
	int afe;
	int sex;
	int others;
	void (*peat)();
	void (*pbeat)();
	void (*test)();

	struct Animal *next;
};
struct Animal* putCatLink(struct Animal *phead);
struct Animal* putDogLink(struct Animal * phead);
struct Animal* putPersonLink(struct Animal *phead);

cat.c

#include "animal.h"

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

void catBeat()
{
	printf("cat beat\n");
}

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

struct Animal* putCatLink(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\n");
}

void dogBeat()
{
	printf("dog beat\n");
}

struct Animal Dog = {
	.name  = "haoshan",
	.peat  = dogEat,
	.pbeat = dogBeat
};

struct Animal* putDogLink(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\n");
}

void personBeat()
{
	printf("person beat\n");
}

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

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

main.c

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

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

        if(phead == NULL){
                printf("NULL\n");
                return NULL;
        }else{
                while(tmp != NULL){
                        if(strcmp(tmp->name,str) == 0){
							printf("strcmp success\n");
                       		return tmp;
                        }
                        tmp = tmp->next;
                }
                return NULL;
        }
}

int main()
{
	
	char buf[128] = {'\0'};
	struct Animal *phead = NULL;
	struct Animal *ptmp;

	phead = putCatLink(phead);
	phead = putDogLink(phead);
	phead = putPersonLink(phead);

	while(1){
			 printf("please input:cat,dog person\n");
			 scanf("%s",buf);
			 ptmp = findUtilByName(buf,phead);
			 if(ptmp != NULL){
			 		printf("success\n");
					ptmp->pbeat();
					ptmp->peat();
			 }
			 memset(buf,'\0',sizeof(buf));
	 }
	

	return 0;
}


运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LJX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值