Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C装饰设计模式(使用pizza示例)_C_Design Patterns - Fatal编程技术网

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

C装饰设计模式(使用pizza示例),c,design-patterns,C,Design Patterns,我正在学习嵌入式系统的低级课程,并被分配了用C语言复制一些设计模式的任务。我让Observator和delegator都在工作,我真的在与decorator模式作斗争。我确实意识到,许多人认为设计模式不属于像C这样的“低级”语言,但我没有选择——要通过这门课程就必须这样做。我找到的所有例子都是面向对象编程语言的。我正在使用这个Java pizza示例作为基础(只是返回成本以使其变得简单),但就我的一生而言,无法让它发挥作用: 这是示例的UML(正如我所说,我只做getCost部分): 我花了大

我正在学习嵌入式系统的低级课程,并被分配了用C语言复制一些设计模式的任务。我让Observator和delegator都在工作,我真的在与decorator模式作斗争。我确实意识到,许多人认为设计模式不属于像C这样的“低级”语言,但我没有选择——要通过这门课程就必须这样做。我找到的所有例子都是面向对象编程语言的。我正在使用这个Java pizza示例作为基础(只是返回成本以使其变得简单),但就我的一生而言,无法让它发挥作用:

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

我花了大约两天的时间试图让它工作,但我只是卡住了。我已经添加了代码,但在如何将西红柿添加到比萨饼中以正确计算成本方面遇到了难题

#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));
}
#包括
typedef结构{
双倍(*getCost)();
}比萨饼;
typedef结构\u toppingDecorator{
双倍(*getCost)();
比萨饼;
}顶级装饰师;
//这些是比萨饼
双倍价格(){
返回5;
}
双倍披萨成本(){
返回7;
}
//这些是浇头
双倍Mozzarella成本(顶级装饰师){
返回self->tempPizza.getCost()+3.0;
}
双tomatoCost(顶级装饰师){
返回self->tempPizza.getCost()+1;
}
int main(int argc,const char*argv[]{
披萨&普通披萨;
plainPizza.getCost=&plainPizzaCost;
比萨饼;
thickcrappizza.getCost=&thickcrappizzacost;
顶级装饰师莫扎里拉;
莫札里拉.坦普比萨=普通比萨;
mozzarella.getCost=&mozzarellaCost;
装饰番茄;
番茄.tempPizza=mozzarella.tempPizza;
tomato.getCost=&tomatoCost;
//现在打印成本
printf(“一个普通比萨饼的价格为%f\n”,plainPizza.getCost());
printf(“一份莫扎里拉比萨饼的价格为%f\n”,mozzarella.getCost(&mozzarella));
printf(“一个西红柿和莫扎里拉比萨饼的价格为%f\n”,tomato.getCost(&mozzarella));
}

不知道为什么这篇文章被否决了,但无论如何。。。一位朋友帮我解决了这个问题,我想我应该把答案贴在这里——谢谢你马塞尔:哦)

#包括
#包括
typedef结构比萨饼;
typedef double(*getCost)(结构*self);
typedef结构{
getCost getCostFunc;
}比萨饼;
类型定义结构{
比萨饼基地;
}朴素的;
typedef结构\u toppingDecorator{
比萨饼基地;
披萨装饰;
}顶级装饰师;
//这些是比萨饼
双倍普通披萨成本(普通披萨自身){
返回5;
}
//这些是浇头
双倍Mozzarella成本(顶级装饰师){
返回自->装饰->获取成本函数(自->装饰)+3;
}
双tomatoCost(顶级装饰师){
返回自->装饰->获取成本函数(自->装饰)+2;
}
双层萨拉米科斯特(顶级装饰师){
返回自->装饰->获取成本函数(自->装饰)+1;
}
int main(int argc,const char*argv[]{
普通披萨\u t普通披萨;
plainPizza.base.getCostFunc=(getCost)plainPizzaCost;
顶级装饰师莫扎里拉;
mozzarella.base.getCostFunc=(getCost)mozzarella成本;
mozzarella.decoration=(pizza\u t*)和plainPizza;
装饰番茄;
tomato.base.getCostFunc=(getCost)tomatoCost;
番茄装饰=(比萨饼)和马苏里拉;
顶级装饰意大利腊肠;
salami.base.getCostFunc=(getCost)salamiCost;
意大利腊肠装饰=(比萨饼*)和西红柿;
printf(“一个番茄比萨饼的价格为%f\n”,tomato.base.getCostFunc((pizza_t*)和tomato));
printf(“一个意大利腊肠比萨的价格为%f\n”,salami.base.getCostFunc((pizza_t*)和salami));
}

多么糟糕的模式:)我知道,我知道。。。但我们需要这样做:)这是装饰图案吗?原因:应该可以使用同一个decorator两次或多个decorator。这有可能制作出番茄+意大利腊肠这样的比萨吗?我不认为在上面的例子中有这种可能性,因为我们只能制作一个西红柿或马苏里拉,但不能同时制作两者。老实说,我不能回答你的问题。我早就完成了这门课程,不再使用C
#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));
}