工厂模式介绍

工厂模式介绍

简单工厂模式

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

	算法不是设计模式,因为算法是解决问题的一种方案,设计模式是解决一类问题。

	例如如下指令:ftp  get  put  cd  mkdir  rm  ls  pwd ......

0.什么是设计模式:

	设计模式通常描述了一组相互紧密作用的类与对象。(java)
	
	 c   语言没有设计模式,只有算法。
	
	java 语言有设计模式,有算法,有面向对象。
	
	建筑设计领域引入了设计模式到计算机科学中来。
	
	23种设计模式:代码更容易被他人理解,保证代码可靠性,程序的重用性。

总体来说设计模式分为三大类:

创建型模式,共5种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式,共7种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

行为型模式,共11种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。

1.什么是类和对象:

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

对象是类的一个实例,对象是类的具体表现。

是一种抽象的数据类型,它是对某一类对象的描述,是具有相同属性和行为的一组对象的集合。

struct Animal  	     //动物
{    
  int age;           //年龄    
  int sex;           //性别       成员属性:int
  void (*peat)();    //eat吃
  void (*pbeat)();   //beat打架   成员方法:void
}

struct Animal dog;   //狗
struct Animal cat;   //猫
struct Animal person;//人

2.什么是工厂模式:

工厂模式(Factory Pattern)是一种创建型设计模式,它提供了一种创建对象最佳方式

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

下面以OOP.c这个程序为例:

//OOP.c file
#include <stdio.h>
//类:抽象
struct Animal
{
	char name[128];
	int age;
	int sex;		//成员属性
	int others;//其他
	void (*peat)();
	void (*pbeat)();//成员方法
	void (*test)();//test测试
};

void dogEat(){	printf("狗吃屎\n");}
void catEat(){	printf("猫吃鱼\n");}
void personEat(){	printf("人吃米\n");}

void dogBeat(){	printf("狗咬兔子\n");}
void catBeat(){	printf("猫抓老鼠\n");}
void personBeat(){	printf("猴子偷桃\n");}

int main()
{
//	struct Animal dog1 = {"阿黄",1,1,100,dogEat,dogBeat,dd};
	
	struct Animal dog ={
		.peat = dogEat,
		.pbeat = dogBeat
	};

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

	struct Animal person ={
		.peat = personEat,
		.pbeat = personBeat
	};  //对象,事务的具象
	
	dog.peat();
	cat.peat();
	person.peat();

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

改为工厂模式后分为以下6个文件:

//main.c file
#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;
		}
		printf("没有找到该动物,请重新输入\n");
		return NULL;
	}	
};

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

	phead = putCatInLink(phead);
	phead = putDogInLink(phead);
	phead = putPersonInLink(phead);
	phead = putMaInLink(phead);

	while (1)
	{
		printf("请输入要查找的动物的名称:Tom,ma,huang,chen\n");
		scanf("%s",buf);
		ptmp = findUtilByName(buf,phead);//查找
		
		if (ptmp != NULL)
		{
		    ptmp->pbeat();
			ptmp->peat();
		}
		memset(buf,'\0',sizeof(buf));
	}
	return 0;
}
//animal.h file  由于函数封装的头文件不多,于是就统一写到animal.h文件里面了。以后大工程每一个.c文件可以都写一一对应的.h文件。
#include <stdio.h>

struct Animal	//类:抽象
{
	char name[128];
	int age;
	int sex;		//成员属性
	int others;//其他
	void (*peat)();
	void (*pbeat)();//成员方法
	void (*test)(); //test测试

    struct Animal *next;	
};

struct  Animal *putCatInLink(struct Animal *phead);
struct  Animal *putDogInLink(struct Animal *phead);
struct  Animal *putPersonInLink(struct Animal *phead);
struct  Animal *putMaInLink(struct Animal *phead);
//person.c file
#include "animal.h"

void personEat(){	printf("人吃米\n");}
void personBeat(){	printf("猴子偷桃\n");}

struct Animal person =
{
	.name = "chen",
	.peat = personEat,
	.pbeat = personBeat
};  //对象,事务的具象

struct  Animal *putPersonInLink(struct Animal *phead)
{
    if (phead == NULL)
    {
        phead = &person;
        return phead;
    }else
    {
        person.next = phead;
        phead = &person;
    }
}
//cat.c file
#include "animal.h"

void catEat()
{	
    printf("猫🐱吃鱼\n");
}
void catBeat()
{	
    printf("猫抓老鼠\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;
    }
}
//dog.c file
#include "animal.h"

void dogEat()
{	
    printf("狗吃屎\n");
}
void dogBeat()
{	
    printf("阿黄🐕咬兔子\n");
}

//	struct Animal dog1 = {"阿黄🐕",1,1,100,dogEat,dogBeat,dd};
struct Animal dog =
{
    .name = "huang",
	.peat = dogEat,
	.pbeat = dogBeat
};

struct  Animal *putDogInLink(struct Animal *phead)
{
    if (phead == NULL)
    {
        phead = &dog;
        return phead;
    }else
    {
        dog.next = phead;
        phead = &dog;
    }
}
//ma.c file
#include "animal.h"

void maEat()
{	
    printf("马🐎吃草\n");
}
void maBeat()
{	
    printf("人骑马🏇\n");
}

struct Animal ma =
{
    .name = "ma",
	.peat = maEat,
	.pbeat = maBeat
};

struct  Animal *putMaInLink(struct Animal *phead)
{
    if (phead == NULL)
    {
        phead = &ma;
        return phead;
    }else
    {
        ma.next = phead;
        phead = &ma;
    }
}
  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式,可以将对象的创建过程与使用过程分离开来。 工厂模式的主要思想是通过一个工厂方法来实现对象的创建,而不是直接在代码中通过new来创建对象。这样做的好处是可以减少代码的耦合度,同时增强代码的可扩展性和可维护性。 举个例子,假设有一个形状接口Shape和三个实现类Circle、Rectangle、Square,我们可以定义一个工厂类ShapeFactory来生成Shape的实现类。 ``` interface Shape { void draw(); } class Circle implements Shape { public void draw() { System.out.println("Circle.draw()"); } } class Rectangle implements Shape { public void draw() { System.out.println("Rectangle.draw()"); } } class Square implements Shape { public void draw() { System.out.println("Square.draw()"); } } class ShapeFactory { public Shape getShape(String shapeType) { if (shapeType == null) { return null; } if (shapeType.equalsIgnoreCase("CIRCLE")) { return new Circle(); } else if (shapeType.equalsIgnoreCase("RECTANGLE")) { return new Rectangle(); } else if (shapeType.equalsIgnoreCase("SQUARE")) { return new Square(); } return null; } } ``` 然后我们可以通过以下代码来生成不同的形状对象: ``` ShapeFactory shapeFactory = new ShapeFactory(); Shape circle = shapeFactory.getShape("CIRCLE"); circle.draw(); // output: Circle.draw() Shape rectangle = shapeFactory.getShape("RECTANGLE"); rectangle.draw(); // output: Rectangle.draw() Shape square = shapeFactory.getShape("SQUARE"); square.draw(); // output: Square.draw() ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值