揭秘C语言中的经典设计模式:轻松实现代码复用与维护的艺术

C语言作为一种历史悠久的编程语言,尽管它没有像Java或C#这样的面向对象语言那样丰富的设计模式库,但仍有许多经典的设计模式可以在C语言中使用。这些模式帮助开发者实现代码的复用性和维护性。以下是几种在C语言中实现经典设计模式的方法。
单例模式(Singleton)
单例模式确保一个类只有一个实例,并提供一个全局访问点。在C语言中,单例模式的实现通常需要全局变量和静态函数。
#include

typedef struct {
// 类成员
} Singleton;

static Singleton instance;

Singleton* getInstance() {
static Singleton instance;
return &instance;
}

int main() {
Singleton* s1 = getInstance();
Singleton* s2 = getInstance();

printf(“Address of s1: %p\n”, (void*)s1);
printf(“Address of s2: %p\n”, (void*)s2);

return 0;
}
在这个例子中,
getInstance
函数返回全局变量
instance
的地址。
工厂模式(Factory)
工厂模式用于创建对象,而不指定具体类。它将对象的创建延迟到子类中进行,使得类能够灵活地创建不同的子类对象。
#include
#include

typedef struct {
void (*print)(void*);
} Product;

typedef struct {
Product* create();
} Factory;

typedef struct {
Factory factory;
} Client;

void concreteProductPrint(void* data) {
printf(“Concrete Product\n”);
}

Product* concreteFactoryCreate() {
Product* product = (Product*)malloc(sizeof(Product));
product->print = concreteProductPrint;
return product;
}

void clientCode(Client* client) {
Product* product = client->factory.create();
product->print(product);
free(product);
}

int main() {
Client client;
client.factory.create = concreteFactoryCreate;

clientCode(&client);

return 0;
}
在这个例子中,
Factory
用于创建具体的
Product
类型的实例。
适配器模式(Adapter)
适配器模式允许将一个类的接口转换成客户期望的另一个接口。这使得原本接口不兼容的类可以一起工作。
#include

typedef struct {
void (*doSomething)(void);
} Target;

typedef struct {
void (*doSomething)(void);
} Adaptee;

typedef struct {
Adaptee adaptee;
Target target;
} Adapter;

void adapteeDoSomething(void) {
printf(“Adaptee doing something\n”);
}

void targetDoSomething(void) {
printf(“Target doing something\n”);
}

void adapterDoSomething(Adapter* adapter) {
adapter->doSomething(&adapter->target);
}

int main() {
Adapter adapter;
adapter.adaptee.doSomething = adapteeDoSomething;
adapter.target.doSomething = targetDoSomething;

adapterDoSomething(&adapter);

return 0;
}
在这个例子中,
Adapter
将
Adaptee
的接口适配到
Target
。
观察者模式(Observer)
观察者模式定义了对象之间的一对多依赖关系,当一个对象改变状态时,所有依赖于它的对象都会得到通知并自动更新。
#include
#include

typedef struct {
void (*update)(void*);
} Observer;

typedef struct {
Observer* observers;
int observerCount;
} Subject;

void observerUpdate(void* observer) {
Observer* obs = (Observer*)observer;
obs->update(obs);
}

void subjectNotify(Subject* subject) {
for (int i = 0; i < subject->observerCount; ++i) {
observerUpdate(subject->observers[i]);
}
}

void concreteObserverUpdate(void* observer) {
Observer* obs = (Observer*)observer;
printf(“Observer %d got notified\n”, obs->observer->observerCount);
}

void concreteSubjectUpdate(void* subject) {
printf(“Subject changed its state\n”);
subjectNotify((Subject*)subject);
}

int main() {
Subject subject;
subject.observers = (Observer*)malloc(2 * sizeof(Observer));
subject.observers[0].update = concreteObserverUpdate;
subject.observers[1].update = concreteObserverUpdate;
subject.observerCount = 2;

subjectNotify(&subject);

free(subject.observers);

return 0;
}
在这个例子中,
Subject
可以通知它的所有
Observer
。
通过这些设计模式,C语言开发者可以构建更易于维护和扩展的代码。虽然C语言本身不直接支持面向对象的概念,但通过上述方式,开发者可以在一定程度上模拟面向对象的行为。