工厂模式介绍
简单工厂模式
代码设计经验的总结,稳定,拓展性更强。一系列编程思想
算法不是设计模式,因为算法是解决问题的一种方案,设计模式是解决一类问题。
例如如下指令: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;
}
}