設(shè)計(jì)模式(簡(jiǎn)單工廠模式)
1.什么是設(shè)計(jì)模式
從建筑設(shè)計(jì)領(lǐng)域引入到計(jì)算機(jī)科學(xué)中
設(shè)計(jì)模式一共有23種
代碼設(shè)計(jì)經(jīng)驗(yàn)的總結(jié),穩(wěn)定,拓展性更強(qiáng)。一系列編程思想
作用:代碼更容易被他人理解、保證代碼可靠性、程序的重用性。
詳細(xì)介紹:[https://www.runoob.com/design-pattern/design-pattern-tutorial.html]:
學(xué)習(xí)簡(jiǎn)單工廠模式的目的:為了代碼不那么混亂。讓代碼更穩(wěn)定,添加功能更方便。
算法:致力于解決問(wèn)題而非設(shè)計(jì)問(wèn)題。
設(shè)計(jì)模式通常描述了一組相互緊密作用的類與對(duì)象。
很多人說(shuō),c是一門(mén)面向過(guò)程的語(yǔ)言,java是一門(mén)面向?qū)ο蟮恼Z(yǔ)言;其實(shí)這種說(shuō)法不太對(duì),因?yàn)槊嫦蜻^(guò)程與面向?qū)ο笾皇且环N代碼編輯的思想。c也可以面向?qū)ο蟆?
2.類和對(duì)象
-
類是一種用戶自定義的數(shù)據(jù)類型,也稱為類類型。
-
(C語(yǔ)言中用戶自定義的數(shù)據(jù)類型是結(jié)構(gòu)體。)
-
對(duì)象:類的具象
例子說(shuō)明:
struct Animal
{
int age;
int sex; //成員屬性
void (*peat)();
void (*pbeat)(); //成員方法(一些具體的行為:比如某某人做了某某事)
};
struct Animal dog;
struct Animal cat;
struct Animal person; //對(duì)上述一種類的具象(即:對(duì)象)
一個(gè)簡(jiǎn)單的面向?qū)ο罄?/strong>
OOP1.c
#include<stdio.h>
//類:抽象
struct Animal
{
char name[32];
int age;
int sex;
void (*peat)();
void (*pbeat)();
};
void dogEat()
{
printf("狗吃屎\n");
}
void catEat()
{
printf("貓吃魚(yú)\n");
}
void personEat()
{
printf("人吃米\n");
}
void dogBeat()
{
printf("咬你\n");
}
void catBeat()
{
printf("摳你\n");
}
void personBeat()
{
printf("打你\n");
}
int main()
{
struct Animal cat; //對(duì)象:事物的具象
struct Animal dog;
struct Animal person;
dog.peat=dogEat; //對(duì)象具體的行為
cat.peat=catEat;
person.peat=personEat;
dog.pbeat=dogBeat;
cat.pbeat=catBeat;
person.pbeat=personBeat;
dogEat();
catEat();
personEat();
dogBeat();
catBeat();
personBeat();
return 0;
}
總結(jié):
類是用戶自定義的數(shù)據(jù)類型。在C語(yǔ)言中是結(jié)構(gòu)體。
類是抽象的,對(duì)象能把事物具象化
成員方法是對(duì)象的具體行為。做到了事務(wù)的具象,每一個(gè)對(duì)象都有具體的行為
在這里回顧一下函數(shù)指針的知識(shí):
每一個(gè)函數(shù)都有一個(gè)入口地址,函數(shù)指針是函數(shù)的入口地址,而函數(shù)名就是函數(shù)的地址。
所以把函數(shù)指針指向函數(shù)名就指向了這個(gè)函數(shù)。這里說(shuō)的有點(diǎn)不好,即:聲明了這個(gè)函數(shù),然后直接調(diào)用。
在回顧一下回調(diào)函數(shù)
定義:
使用者自己定義一個(gè)函數(shù),實(shí)現(xiàn)這個(gè)函數(shù)的程序內(nèi)容,然后把這個(gè)函數(shù)(入口地址)作為參數(shù)傳入別人(或系統(tǒng))的函數(shù)中,由別人(或系統(tǒng))的函數(shù)在運(yùn)行時(shí)來(lái)調(diào)用的函數(shù)。
函數(shù)是你實(shí)現(xiàn)的,但由別人(或系統(tǒng))的函數(shù)在運(yùn)行時(shí)通過(guò)參數(shù)傳遞的方式調(diào)用,這就是所謂的回調(diào)函數(shù)。
簡(jiǎn)單來(lái)說(shuō),當(dāng)發(fā)生某種事件時(shí),系統(tǒng)或其他函數(shù)將會(huì)自動(dòng)調(diào)用你定義的一段函數(shù)。
示例:
回調(diào)函數(shù)主要結(jié)構(gòu)有三部分組成:主函數(shù)、調(diào)用函數(shù)和被調(diào)函數(shù)。C語(yǔ)言中,被調(diào)函數(shù)通常以函數(shù)指針(指向?qū)?yīng)函數(shù)的入口地址)的形式出現(xiàn)。
//定義回調(diào)函數(shù)
void PrintfText()
{
printf("Hello World!\n");
}
//定義實(shí)現(xiàn)回調(diào)函數(shù)的"調(diào)用函數(shù)"
// 參數(shù)為函數(shù)指針,無(wú)參數(shù)返回void
void CallPrintfText(void (*callfuct)())
{
callfuct();
}
//實(shí)現(xiàn)函數(shù)回調(diào)
int main(int argc,char* argv[])
{
CallPrintfText(PrintfText);
return 0;
}
2.2C結(jié)構(gòu)體新玩法
2.2.1常用的方法
struct Animal
{
char name[32];
int age;
int sex;
int others;
void (*peat)();
void (*pbeat)();
};
void dogEat()
{
printf("狗吃屎\n");
}
void catEat()
{
printf("貓吃魚(yú)\n");
}
void personEat()
{
printf("人吃米\n");
}
void dogBeat()
{
printf("咬你\n");
}
void catBeat()
{
printf("摳你\n");
}
void personBeat()
{
printf("打你\n");
}
int main()
{
struct Animal cat={"阿黃",18,‘b’,100,dogeat,dogBeat};
return 0;
}
2.2.2新玩法(內(nèi)核常用)
struct Animal
{
char name[32];
int age;
int sex;
int others;
void (*peat)();
void (*pbeat)();
};
void dogEat()
{
printf("狗吃屎\n");
}
void catEat()
{
printf("貓吃魚(yú)\n");
}
void personEat()
{
printf("人吃米\n");
}
void dogBeat()
{
printf("咬你\n");
}
void catBeat()
{
printf("摳你\n");
}
void personBeat()
{
printf("打你\n");
}
int main()
{
struct Animal cat={
.peat=catEat;
.pbeat=catBeat
};
struct Animal dog={
.peat=dogEat;
.pbeat=dogBeat
};
struct Animal person={
.peat=personEat;
.pbeat=personBeat
};
return 0;
}
3.工廠模式
3.1.什么是工廠模式
工廠模式是最常用的設(shè)計(jì)模式之一。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,他提供了一種創(chuàng)建對(duì)象的最佳方式。
在工廠模式中,我們創(chuàng)建對(duì)象時(shí)不會(huì)對(duì)客戶端暴露創(chuàng)建邏輯,并且時(shí)通過(guò)使用一個(gè)共同的接口來(lái)指向新創(chuàng)建的對(duì)象
暴露創(chuàng)建邏輯是:暴露了對(duì)象的具體指向和 函數(shù)的實(shí)現(xiàn)方式。
不在業(yè)務(wù)中暴露,不在文件中暴露,在其他地方做好,調(diào)用。
共同的接口:做好API放到工廠里面,然后mian函數(shù)里調(diào)用
示例:
dog.c
#include "animal.h"
void dogEat()
{
printf("狗吃屎\n");
}
void dogBeat()
{
printf("咬你\n");
}
struct Animal dog={
.name="huang",
.peat=dogEat,
.pbeat=dogBeat
};
struct Animal* putDogInLink(struct Animal *phead)
{
if(phead==NULL)
{
return &dog;
}
else
{
dog.next=phead;
phead=&dog;
return phead;
}
}
cat.c
#include "animal.h"
void catEat()
{
printf("貓吃魚(yú)\n");
}
void catBeat()
{
printf("摳你\n");
}
struct Animal cat={
.name="Tom",
.peat=catEat,
.pbeat=catBeat
};
struct Animal* putCatInLink(struct Animal *phead)
{
if(phead==NULL)
{
return &cat;
}
else
{
cat.next=phead;
phead=&cat;
return phead;
}
}
fish.c
#include "animal.h"
void fishEat()
{
printf("魚(yú)吃料\n");
}
void fishBeat()
{
printf("瞪你\n");
}
struct Animal fish={
.name="fish",
.peat=fishEat,
.pbeat=fishBeat
};
struct Animal* putFishInLink(struct Animal *phead)
{
if(phead==NULL)
{
return &fish;
}
else
{
fish.next=phead;
phead=&fish;
return phead;
}
}
ma.c
#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)
{
return &ma;
}
else
{
ma.next=phead;
phead=&ma;
return phead;
}
}
person.c
#include "animal.h"
void personEat()
{
printf("人吃米\n");
}
void personBeat()
{
printf("打你\n");
}
struct Animal person={
.name="xiaoming",
.peat=personEat,
.pbeat=personBeat
};
struct Animal* putPersonInLink(struct Animal *phead)
{
if(phead==NULL)
{
return &person;
}
else
{
person.next=phead;
phead=&person;
return phead;
}
}
mianPro.c
#include "animal.h"
struct Animal* findUtilName(char *str,struct Animal *phead)
{
struct Animal *ptmp=phead;
if(phead==NULL)
{
printf("鏈表為空,無(wú)法查找\n");
return NULL;
}
while(ptmp!=NULL)
{
// printf("哥,你輸?shù)闹噶钍钦_的\n");
if(strcmp(ptmp->name,str)==0)
{
return ptmp;
}
ptmp=ptmp->next;
}
printf("你輸錯(cuò)了,大哥,指令不對(duì)\n");
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);
phead=putMaInLink(phead);
phead=putFishInLink(phead);
while(1)
{
memset(buf,0,sizeof(buf));
printf("please input Tom,huang,xiaoming,ma,fish;quit exit\n");
scanf("%s",buf);
ptmp=findUtilName(buf,phead);
if(ptmp!=NULL)
{
ptmp->peat();
ptmp->pbeat();
}
if(strcmp(buf,"quit")==0)
{
printf("你已退出\n");
exit(-1);
}
}
return 0;
}
animal.h文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-541469.html
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
struct Animal
{
char name[32];
int age;
int sex;
int others;
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);
struct Animal* putMaInLink(struct Animal *phead);
struct Animal* putFishInLink(struct Animal *phead);
工廠里有貓狗魚(yú)人馬,用鏈表串起來(lái),業(yè)務(wù)邏輯放在mianpro.c,animal.h暴露出來(lái),然后mianpro調(diào)用,這樣代碼會(huì)很穩(wěn)定,也不亂,一個(gè)功能放一個(gè)文件文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-541469.html
到了這里,關(guān)于設(shè)計(jì)模式(簡(jiǎn)單工廠模式)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!