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就被添加进去了
这边博文写的更加详细感兴趣的小伙伴可以去参考————设计模式——简易工厂模式