国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

設(shè)計(jì)模式(簡(jiǎn)單工廠模式)

這篇具有很好參考價(jià)值的文章主要介紹了設(shè)計(jì)模式(簡(jiǎn)單工廠模式)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

設(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

#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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 設(shè)計(jì)模式(簡(jiǎn)單工廠模式)

    從建筑設(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)單工廠模式的目的:

    2024年02月13日
    瀏覽(18)
  • 聊聊設(shè)計(jì)模式--簡(jiǎn)單工廠模式

    聊聊設(shè)計(jì)模式--簡(jiǎn)單工廠模式

    ? 前面也學(xué)了很多各種微服務(wù)架構(gòu)的組件,包括后續(xù)的服務(wù)部署、代碼管理、Docker等技術(shù),那么作為后端人員,最重要的任務(wù)還是代碼編寫(xiě)能力,如何讓你的代碼寫(xiě)的漂亮、易擴(kuò)展,讓別人一看賞心悅目,那么設(shè)計(jì)模式就是很重的了。那么本本篇文章就來(lái)聊聊一個(gè)簡(jiǎn)單的工廠

    2024年02月07日
    瀏覽(24)
  • JavaScript設(shè)計(jì)模式(二)——簡(jiǎn)單工廠模式、抽象工廠模式、建造者模式

    JavaScript設(shè)計(jì)模式(二)——簡(jiǎn)單工廠模式、抽象工廠模式、建造者模式

    個(gè)人簡(jiǎn)介 ?? 個(gè)人主頁(yè): 前端雜貨鋪 ???♂? 學(xué)習(xí)方向: 主攻前端方向,正逐漸往全干發(fā)展 ?? 個(gè)人狀態(tài): 研發(fā)工程師,現(xiàn)效力于中國(guó)工業(yè)軟件事業(yè) ?? 人生格言: 積跬步至千里,積小流成江海 ?? 推薦學(xué)習(xí):??前端面試寶典 ??Vue2 ??Vue3 ??Vue2/3項(xiàng)目實(shí)戰(zhàn) ??Node.js??

    2024年02月10日
    瀏覽(23)
  • [設(shè)計(jì)模式]創(chuàng)建型模式-簡(jiǎn)單工廠模式

    簡(jiǎn)單工廠模式又稱為靜態(tài)工廠模式,屬于創(chuàng)建型模式,但不屬于GOF23設(shè)計(jì)模式。由一個(gè)工廠對(duì)象決定創(chuàng)建出哪一種產(chǎn)品類的實(shí)例。簡(jiǎn)單工廠模式的實(shí)質(zhì)是由一個(gè)工廠類根據(jù)傳入的參數(shù),動(dòng)態(tài)決定應(yīng)該創(chuàng)建哪一個(gè)產(chǎn)品類。 簡(jiǎn)單工廠適用場(chǎng)景:工廠類負(fù)責(zé)創(chuàng)建的對(duì)象比較少;客戶

    2024年02月20日
    瀏覽(19)
  • [設(shè)計(jì)模式] 簡(jiǎn)單工廠模式簡(jiǎn)易案例

    工廠模式是一種創(chuàng)建型設(shè)計(jì)模式,用于根據(jù)不同條件或參數(shù)創(chuàng)建不同類型的對(duì)象。 解決的痛點(diǎn):當(dāng)一個(gè)類不知道它必須創(chuàng)建哪個(gè)對(duì)象時(shí),工廠模式可以用來(lái)創(chuàng)建特定類型的對(duì)象。 以下是一個(gè)簡(jiǎn)單的工廠模式的 Java 示例,假設(shè)你要?jiǎng)?chuàng)建不同類型的形狀對(duì)象。 首先,定義一個(gè)抽

    2024年02月09日
    瀏覽(29)
  • 設(shè)計(jì)模式之~工廠系列(簡(jiǎn)單工廠、工廠方法、抽象工廠)

    設(shè)計(jì)模式之~工廠系列(簡(jiǎn)單工廠、工廠方法、抽象工廠)

    目錄 簡(jiǎn)單工廠模式 工廠方法模式 簡(jiǎn)單工廠 VS 工廠方法 抽象工廠模式: 拓展: 利用簡(jiǎn)單工廠模式優(yōu)化抽象工廠?? 利用反射+抽象工廠? ?進(jìn)行優(yōu)化 反射+配置文件+抽象工廠進(jìn)行優(yōu)化 優(yōu)點(diǎn):簡(jiǎn)單工廠模式的最大優(yōu)點(diǎn)在于工廠類包含了必要的邏輯判斷,根據(jù)客戶端的選擇條件動(dòng)

    2024年02月07日
    瀏覽(24)
  • 設(shè)計(jì)模式:簡(jiǎn)單工廠、工廠方法、抽象工廠的區(qū)別

    描述: 簡(jiǎn)單工廠模式并不是嚴(yán)格意義上的設(shè)計(jì)模式,而更像是一種編程習(xí)慣或者說(shuō)是一種創(chuàng)建對(duì)象的簡(jiǎn)單方式。它使用一個(gè)工廠類來(lái)創(chuàng)建對(duì)象,這個(gè)工廠類包含一個(gè)方法,根據(jù)輸入的參數(shù)或條件來(lái)創(chuàng)建相應(yīng)的對(duì)象實(shí)例。 舉例: 描述: 工廠方法模式是一種創(chuàng)建型模式,它定義

    2024年01月21日
    瀏覽(22)
  • 探索設(shè)計(jì)模式的魅力:簡(jiǎn)單工廠模式

    探索設(shè)計(jì)模式的魅力:簡(jiǎn)單工廠模式

    簡(jiǎn)單工廠模式(Simple Factory Pattern)是一種創(chuàng)建型設(shè)計(jì)模式,其主要目的是用于創(chuàng)建對(duì)象的實(shí)例。這種模式通過(guò)封裝創(chuàng)建對(duì)象的代碼來(lái)降低客戶代碼與具體類之間的耦合度。簡(jiǎn)單工廠不是GoF(四人幫)設(shè)計(jì)模式之一,但它是一個(gè)常用的編程慣用法。 在簡(jiǎn)單工廠模式中,創(chuàng)建對(duì)

    2024年01月17日
    瀏覽(33)
  • 設(shè)計(jì)模式01———簡(jiǎn)單工廠模式 c#

    設(shè)計(jì)模式01———簡(jiǎn)單工廠模式 c#

    首先我們打開(kāi)一個(gè)項(xiàng)目 在這個(gè)初始界面我們需要做一些準(zhǔn)備工作 建基礎(chǔ)通用包 創(chuàng)建一個(gè)Plane 重置后 縮放100倍 加一個(gè)顏色 任務(wù):使用【簡(jiǎn)單工廠模式】生成四種不同怪物 【按不同路徑移動(dòng)】 首先資源商店下載四個(gè)怪物模型 接下來(lái)我們選取四個(gè)怪物作為預(yù)制體并分別起名為

    2024年02月07日
    瀏覽(20)
  • 《設(shè)計(jì)模式的藝術(shù)》筆記 - 簡(jiǎn)單工廠模式

    《設(shè)計(jì)模式的藝術(shù)》筆記 - 簡(jiǎn)單工廠模式

    ? ? ? ? 定義一個(gè)工廠類,它可以根據(jù)參數(shù)的不同返回不同類的實(shí)例,被創(chuàng)建的實(shí)例通常都具有相同的父類。因?yàn)樵诤?jiǎn)單工廠模式中用于創(chuàng)建實(shí)例的方法是靜態(tài)方法,因此簡(jiǎn)單工廠模式又被稱為靜態(tài)工廠方法模式,屬于類創(chuàng)建型模式 ? ? ? ? 將Factory合并到父類Product中,此時(shí)

    2024年01月16日
    瀏覽(21)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包