網(wǎng)站制作多久能完成廣州優(yōu)化疫情防控措施
C語(yǔ)言設(shè)計(jì)模式
盡管 C 語(yǔ)言并不直接支持面向?qū)ο缶幊?#xff0c;但通過結(jié)構(gòu)體和函數(shù)指針的靈活運(yùn)用,我們依然可以實(shí)現(xiàn)多種經(jīng)典的設(shè)計(jì)模式。
1. 工廠模式
1.1 工廠方法的定義與實(shí)現(xiàn)
工廠模式通過統(tǒng)一的接口創(chuàng)建對(duì)象,客戶端無需知道具體的創(chuàng)建邏輯。
代碼示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>// 抽象產(chǎn)品類型定義
typedef struct Product {void (*use)(struct Product*); // 指向具體產(chǎn)品使用方法的函數(shù)指針
} Product;// 具體產(chǎn)品A定義
typedef struct {Product base;char* name;
} ProductA;// 產(chǎn)品A的使用方法實(shí)現(xiàn)
void useProductA(Product* base) {ProductA* self = (ProductA*)base;printf("Using Product A: %s\n", self->name);
}// 具體產(chǎn)品B定義
typedef struct {Product base;int version;
} ProductB;// 產(chǎn)品B的使用方法實(shí)現(xiàn)
void useProductB(Product* base) {ProductB* self = (ProductB*)base;printf("Using Product B: version %d\n", self->version);
}// 工廠方法,根據(jù)類型創(chuàng)建不同的產(chǎn)品
Product* createProduct(const char* type) {if (strcmp(type, "A") == 0) {ProductA* product = malloc(sizeof(ProductA));product->base.use = useProductA;product->name = "Example A";return (Product*)product;} else if (strcmp(type, "B") == 0) {ProductB* product = malloc(sizeof(ProductB));product->base.use = useProductB;product->version = 1;return (Product*)product;}return NULL;
}// 測(cè)試工廠模式
int main() {Product* p1 = createProduct("A");Product* p2 = createProduct("B");p1->use(p1);p2->use(p2);free(p1);free(p2);return 0;
}
1.2 適用場(chǎng)景
- 數(shù)據(jù)庫(kù)連接:根據(jù)需求創(chuàng)建不同類型的數(shù)據(jù)庫(kù)連接對(duì)象。
- 圖形系統(tǒng):動(dòng)態(tài)創(chuàng)建圓形、矩形等不同的圖形對(duì)象。
- 日志系統(tǒng):創(chuàng)建不同日志記錄方式的對(duì)象,如文件日志或控制臺(tái)日志。
2. 策略模式
2.1 策略模式的實(shí)現(xiàn)
策略模式通過函數(shù)指針實(shí)現(xiàn)不同策略的切換。
代碼示例:
#include <stdio.h>
#include <stdlib.h>// 策略類型定義
typedef struct {void (*execute)(void); // 指向策略實(shí)現(xiàn)的函數(shù)指針
} Strategy;// 策略A實(shí)現(xiàn)
void strategyA() {printf("Executing Strategy A\n");
}// 策略B實(shí)現(xiàn)
void strategyB() {printf("Executing Strategy B\n");
}// 測(cè)試策略模式
int main() {Strategy strategy;// 使用策略Astrategy.execute = strategyA;strategy.execute();// 切換為策略Bstrategy.execute = strategyB;strategy.execute();return 0;
}
2.2 適用場(chǎng)景
- 排序算法:動(dòng)態(tài)選擇快速排序、歸并排序等不同算法。
- 支付系統(tǒng):支持支付寶、微信支付等多種支付方式切換。
- 文件壓縮:選擇不同的壓縮算法,如ZIP或RAR。
3. 觀察者模式
3.1 觀察者模式的實(shí)現(xiàn)
觀察者模式實(shí)現(xiàn)一對(duì)多的通知機(jī)制。
代碼示例:
#include <stdio.h>
#include <stdlib.h>// 觀察者接口定義
typedef struct Observer {void (*update)(struct Observer*, const char* message); // 更新方法struct Observer* next; // 鏈表指針,指向下一個(gè)觀察者
} Observer;// 具體觀察者定義
typedef struct {Observer base;char* name;
} ConcreteObserver;// 具體觀察者的更新方法實(shí)現(xiàn)
void observerUpdate(Observer* base, const char* message) {ConcreteObserver* self = (ConcreteObserver*)base;printf("%s received: %s\n", self->name, message);
}// 主題(Subject)定義
typedef struct {Observer* observers; // 鏈表頭指針,保存所有觀察者
} Subject;// 添加觀察者到主題
void addObserver(Subject* subject, Observer* observer) {observer->next = subject->observers;subject->observers = observer;
}// 通知所有觀察者
void notifyObservers(Subject* subject, const char* message) {Observer* current = subject->observers;while (current) {current->update(current, message);current = current->next;}
}// 測(cè)試觀察者模式
int main() {Subject subject = { .observers = NULL };// 創(chuàng)建兩個(gè)觀察者ConcreteObserver o1 = { .base.update = observerUpdate, .name = "Observer 1" };ConcreteObserver o2 = { .base.update = observerUpdate, .name = "Observer 2" };// 將觀察者添加到主題中addObserver(&subject, (Observer*)&o1);addObserver(&subject, (Observer*)&o2);// 通知所有觀察者notifyObservers(&subject, "Event happened");return 0;
}
3.2 適用場(chǎng)景
- GUI事件:按鈕點(diǎn)擊后通知多個(gè)事件監(jiān)聽器。
- 實(shí)時(shí)數(shù)據(jù)監(jiān)控:股票價(jià)格變動(dòng)后通知多個(gè)用戶。
- 發(fā)布-訂閱系統(tǒng):如聊天室中用戶訂閱消息后實(shí)時(shí)接收。