C语言和设计模式(之组合模式)

【 声明:版权所有,欢迎转载,请勿用于商业用途。  联系信箱:feixiaoxing @163.com】

    组合模式听说去很玄乎,其实也并不复杂。为什么?大家可以先想一下数据结构里面的二叉树是怎么回事。为什么就是这么一个简单的二叉树节点既可能是叶节点,也可能是父节点?

typedef struct _NODE
{
    void* pData;
    struct _NODE* left;
    struct _NODE* right;
}NODE;
    那什么时候是叶子节点,其实就是left、right为NULL的时候。那么如果它们不是NULL呢,那么很明显此时它们已经是父节点了。那么,我们的这个组合模式是怎么一个情况呢?

typedef struct _Object
{
    struct _Object** ppObject;
    int number;
    void (*operate)(struct _Object* pObject);

}Object;
    就是这么一个简单的数据结构,是怎么实现子节点和父节点的差别呢。比如说,现在我们需要对一个父节点的operate进行操作,此时的operate函数应该怎么操作呢?
void operate_of_parent(struct _Object* pObject)
{
    int index;
    assert(NULL != pObject);
    assert(NULL != pObject->ppObject && 0 != pObject->number);

    for(index = 0; index < pObject->number; index ++)
    {
        pObject->ppObject[index]->operate(pObject->ppObject[index]);
    }
}   
    当然,有了parent的operate,也有child的operate。至于是什么操作,那就看自己是怎么操作的了。

void operate_of_child(struct _Object* pObject)
{
    assert(NULL != pObject);
    printf("child node!\n");
}
    父节点也好,子节点也罢,一切的一切都是最后的应用。其实,用户的调用也非常简单,就这么一个简单的函数。

void process(struct Object* pObject)
{
    assert(NULL != pObject);
    pObject->operate(pObject);
}


  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
当然可以。以下是一个使用C语言编写的嵌入式硬件设计模式示例: ``` #include <stdio.h> // 建立一个抽象基类 typedef struct { void (*operation)(void); } Component; // 建立一个具体的类 typedef struct { Component component; char *name; } ConcreteComponent; void ConcreteComponent_operation(void) { printf("ConcreteComponent_operation\n"); } // 继承抽象基类并重写方法 typedef struct { Component component; Component *component_ptr; } Decorator; void Decorator_operation(void) { Decorator *decorator = (Decorator*)this; if (decorator->component_ptr != NULL) { decorator->component_ptr->operation(); } } // 建立一个具体的修饰器 typedef struct { Decorator decorator; char *name; } ConcreteDecorator; void ConcreteDecorator_operation(void) { ConcreteDecorator *concrete_decorator = (ConcreteDecorator*)this; printf("ConcreteDecorator_operation: %s\n", concrete_decorator->name); Decorator_operation(this); } int main() { // 创建具体对象 ConcreteComponent concrete_component = { .component = { .operation = ConcreteComponent_operation }, .name = "ConcreteComponent" }; printf("ConcreteComponent:\n"); concrete_component.component.operation(); // 创建修饰器对象 Decorator decorator = { .component = { .operation = Decorator_operation }, .component_ptr = NULL }; ConcreteDecorator concrete_decorator = { .decorator = decorator, .name = "ConcreteDecorator" }; concrete_decorator.decorator.component_ptr = &concrete_component.component; printf("ConcreteDecorator:\n"); concrete_decorator.decorator.component.operation(); return 0; } ``` 这个示例实现了装饰器模式,通过继承抽象基类 Component 和 Decorator 并重写方法来实现具体的类和修饰器。在 main 函数中创建了一个具体对象和一个修饰器对象,并将它们组合在一起。最终输出了它们的操作结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嵌入式-老费

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值