C装饰设计模式(使用披萨示例)

时间:2016-03-14 20:01:46

标签: c design-patterns

我正在从事嵌入式系统的低级课程,并且已经被分配了在C中复制一些设计模式的任务。我已经让观察者和委托人工作了我真的在装饰模式上苦苦挣扎。我确实认识到许多人认为设计模式不属于像C这样的“低级”语言,但我没有选择 - 需要完成这个课程。我发现的所有示例都是针对OO编程语言的。我正在使用这个Java比萨示例作为基础(只需返回成本以使其变得简单)但是对于我的生活无法让它工作:http://www.newthinktank.com/2012/09/decorator-design-pattern-tutorial/

这是示例的UML(正如我所说,我只是在做getCost部分):

Decorator Pattern for Pizza example

我花了大约2天试图让这个工作,但我只是卡住了。我已经添加了我的代码,但是我很难将番茄添加到披萨中,以便正确加入成本

#include <stdio.h>

typedef struct _pizza {
    double (* getCost) ();
} pizza_t;

typedef struct _toppingDecorator {
    double (* getCost) ();
    pizza_t tempPizza;
} toppingDecorator_t;

// these are the pizzas
double plainPizzaCost () {
    return 5;
}
double thickCrustPizzaCost () {
    return 7;
}

// these are the toppings
double mozzarellaCost (toppingDecorator_t * self) {
    return self->tempPizza.getCost () + 3.0;
}
double tomatoCost (toppingDecorator_t * self) {
    return self->tempPizza.getCost () + 1;
}

int main(int argc, const char * argv[]) {

    pizza_t plainPizza;
    plainPizza.getCost = &plainPizzaCost;

    pizza_t thickCrustPizza;
    thickCrustPizza.getCost = &thickCrustPizzaCost;

    toppingDecorator_t mozzarella;
    mozzarella.tempPizza = plainPizza;
    mozzarella.getCost = &mozzarellaCost;

    toppingDecorator_t tomato;
    tomato.tempPizza = mozzarella.tempPizza;
    tomato.getCost = &tomatoCost;

    // now print the cost
    printf ("A plain pizza costs %f\n", plainPizza.getCost ());
    printf ("A mozzarella pizza costs %f\n", mozzarella.getCost (&mozzarella));
    printf ("A tomato and mozzarella pizza costs %f\n", tomato.getCost (&mozzarella));
}

1 个答案:

答案 0 :(得分:2)

不确定为什么这个帖子被投票但无论如何......一位朋友为我解决了这个问题,我想我会在这里发布答案 - 谢谢Marcel:o)

#include <stdio.h>
#include <stdlib.h>

typedef struct _pizza pizza_t;
typedef double (* getCost)(struct _pizza * self);

typedef struct _pizza {
    getCost getCostFunc;
} pizza_t;

typedef struct _plainPizza {
    pizza_t base;
} plainPizza_t;

typedef struct _toppingDecorator {
    pizza_t base;
    pizza_t * decorate;
} toppingDecorator_t;

// these are the pizzas
double plainPizzaCost (plainPizza_t self) {
    return 5;
}

// these are the toppings
double mozzarellaCost (toppingDecorator_t * self) {
    return self->decorate->getCostFunc(self->decorate) + 3;
}
double tomatoCost (toppingDecorator_t * self) {
    return self->decorate->getCostFunc(self->decorate) + 2;
}
double salamiCost (toppingDecorator_t * self) {
    return self->decorate->getCostFunc(self->decorate) + 1;
}

int main(int argc, const char * argv[]) {

    plainPizza_t plainPizza;
    plainPizza.base.getCostFunc = (getCost) plainPizzaCost;

    toppingDecorator_t mozzarella;
    mozzarella.base.getCostFunc = (getCost) mozzarellaCost;
    mozzarella.decorate = (pizza_t *) &plainPizza;

    toppingDecorator_t tomato;
    tomato.base.getCostFunc = (getCost) tomatoCost;
    tomato.decorate = (pizza_t *) &mozzarella;

    toppingDecorator_t salami;
    salami.base.getCostFunc = (getCost) salamiCost;
    salami.decorate = (pizza_t *) &tomato;

    printf ("A tomato pizza costs %f\n", tomato.base.getCostFunc((pizza_t *) &tomato));
    printf ("A salami pizza costs %f\n", salami.base.getCostFunc((pizza_t *) &salami));
}