1.什么是设计模式
是前人代码设计经验的总结,稳定,拓展性更强,代码更易理解。它是一系列编程思想。
一共有23种,工厂模式是其中之一。
为什么要用设计模式?
有时写代码添加功能的时候,会导致程序其他部分的代码被破坏。而用设计模式的话,能让代码更稳定,拓展性更强。
算法不是设计模式,因为算法致力于解决问题而非设计问题。设计模式通常描述了一组相互紧密作用的类与对象。
2.什么是类和对象
类是一种用户定义的引用数据类型,也称类类型。C语言中就是结构体
对象:类的一种具象
struct Animal{
int age;
int sex; //成员属性
void *peat();
void *pbeat(); //成员方法
};
struct Animal dog;
struct Animal cat;
struct Animal person;
dog,cat,person,都是Animal类的一种对象
#include <stdio.h>
//类:抽象
struct Animal
{
char name[32];
int age;
int sex; //成员属性
void (*peat)();
void (*pbeat)(); //成员方法
};
void dogEat(){
printf("狗吃屎\n");
}
void catEat(){
printf("猫吃鱼\n");
}
void personEat(){
printf("人吃米\n");
}
int main()
{
struct Animal dog; //对象:事物的具象;
struct Animal cat;
struct Animal person;
dog.peat = dogEat;
cat.peat = catEat;
person.peat = personEat;
//调用
dog.peat();
cat.peat();
person.peat();
return 0;
}
下面也是结构体另一种赋值方式:
struct Animal dog = {
.peat = dogEat
}
struct Animal cat = {
.peat = catEat
}
struct Animal person = {
.peat = person
}
这也是结构体的一种赋值方式,并且做到了可以只对指定成员赋值
3.什么是工厂模式?
工厂模式(Factory Pattern)是 常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
我们想用设计模式中的工厂模式来构建智能家居的代码。
其实上面我们给dog,cat,person这三个对象赋值的方式太LOW,在工厂模式中,我们就不用这种方式赋值,而是用一种更佳的方式。
另外上面代码中:
.peat = dogEat
.peat = catEat
这种赋值方式就是在暴露创建逻辑。而工厂模式都会隐藏起来,并且是通过使用一个共同的接口来指向新创建的对象。
示例代码:
animal.h
#include<stdio.h>
struct Animal
{
char name[32];
int age;
int sex; //成员属性
void (*peat)();
void (*pbeat)(); //成员方法
void (*test)();
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"
void catEat(){
printf("cat eat\n");
}
void catBeat(){
printf("cat beat\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;
return phead;
}
}
dog.c
#include"animal.h"
void dogEat(){
printf("dog eat\n");
}
void dogBeat(){
printf("dog beat\n");
}
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;
return phead;
}
}
person.c
#include"animal.h"
void personEat(){
printf("person eat\n");
}
void personBeat(){
printf("person beat\n");
}
struct Animal person = {
.name = "lit",
.peat = personEat,
.pbeat = personBeat
};
struct Animal *putPersonInLink(struct Animal *phead)
{
if(phead == NULL){
phead = &person;
return phead;
}else{
person.next = phead;
phead = &person;
return phead;
}
}
mainPro.c
#include"animal.h"
#include"string.h"
struct Animal *findUtilByName(char *str, struct Animal *phead)//传的名字和头节点
{
struct Animal *tmp = phead;
if(tmp == NULL){
printf("error in link\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 *ptmp = NULL;
phead = putCatInLink(phead);
phead = putDogInLink(phead);
phead = putPersonInLink(phead);
while(1){
printf("input:tom,huang,lit\n");
scanf("%s", buf);
ptmp = findUtilByName(buf, phead);//查找节点 根据特定的条件,在工厂里面去找
if(ptmp != NULL){ //找到后 拿来执行
ptmp -> pbeat();
ptmp -> peat();
}
memset(buf,'\0', sizeof(buf));
}
return 0;
}
这样的设计就让代码做到了更加稳定,更简洁。并且我们如果要加另一种对象时,只需在工厂里加入相应的.c文件,mainPro里拿来执行就行,对整体代码改动较小。
运行结果: