本文介绍了请帮我理解“装饰图案”以真实的世界为例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在中记录的装饰模式。对我来说似乎是一个复杂的设计模式。



所以请帮我理解装饰模式。有人可以给出在现实世界中有用的用例吗?

解决方案

比萨店的情况。在比萨饼店里,他们会卖很少的比萨饼,他们也会在菜单上提供浇头。现在想象一个情况,如果比萨店必须为比萨饼和顶部的每个组合提供价格。即使有四个基本的比萨饼和八个不同的浇头,应用程序会疯狂地保持所有这些具体的比萨饼和浇头组合。



这里是装饰图案。 p>

根据装饰图案,您将实施浇头作为装饰师,比萨饼将由这些浇头的装饰师装饰。实际上每个客户都希望自己的愿望和最终的帐单数量将由基地比萨饼和另外订购的浇头组成。每个顶级装饰师都会知道它正在装饰的比萨饼,这是价格。顶部对象的GetPrice()方法将返回比萨和顶部的累积价格。



编辑



  public abstract class BasePizza 
{
protected double myPrice;

public virtual double GetPrice()
{
return this.myPrice;
}
}

public abstract class ToppingsDecorator:BasePizza
{
protected BasePizza pizza;
public ToppingsDecorator(BasePizza pizzaToDecorate)
{
this.pizza = pizzaToDecorate;
}

public override double GetPrice()
{
return(this.pizza.GetPrice()+ this.myPrice);
}
}

类程序
{
[STAThread]
static void Main()
{
//客户端代码
Margherita pizza = new Margherita();
Console.WriteLine(Plain Margherita:+ pizza.GetPrice()。ToString());

ExtraCheeseTopping moreCheese = new ExtraCheeseTopping(pizza);
ExtraCheeseTopping someMoreCheese = new ExtraCheeseTopping(moreCheese);
Console.WriteLine(Plain Margherita with double extra cheese:+ someMoreCheese.GetPrice()。ToString());

MushroomTopping moreMushroom = new MushroomTopping(someMoreCheese);
Console.WriteLine(Plain Margherita with double extra cheese with mushroom:+ moreMushroom.GetPrice()。ToString());

JalapenoTopping moreJalapeno = new JalapenoTopping(moreMushroom);
Console.WriteLine(Plain Margherita with double extra cheese with mushroom with Jalapeno)+ moreJalapeno.GetPrice()。ToString());

Console.ReadLine();
}
}

public class Margherita:BasePizza
{
public Margherita()
{
this.myPrice = 6.99 ;
}
}

public class美食:BasePizza
{
public Gourmet()
{
this.myPrice = 7.49 ;
}


public class ExtraCheeseTopping:ToppingsDecorator
{
public ExtraCheeseTopping(BasePizza pizzaToDecorate)
:base(pizzaToDecorate)
{
this.myPrice = 0.99;
}
}

public class MushroomTopping:ToppingsDecorator
{
public MushroomTopping(BasePizza pizzaToDecorate)
:base(pizzaToDecorate)
{
this.myPrice = 1.49;
}


public class JalapenoTopping:ToppingsDecorator
{
public JalapenoTopping(BasePizza pizzaToDecorate)
:base(pizzaToDecorate)
{
this.myPrice = 1.49;
}
}


I was studying the Decorator Pattern as documented in GOF. It seems like a complicated design pattern to me.

So please, help me understand the Decorator Pattern. Could someone give a use-case example of where this is useful in the real world?

解决方案

Consider a case of a pizza shop. In the pizza shop they will sell few pizza varieties and they will also provide toppings in the menu. Now imagine a situation wherein if the pizza shop has to provide prices for each combination of pizza and topping. Even if there are four basic pizzas and 8 different toppings, the application would go crazy maintaining all these concrete combination of pizzas and toppings.

Here comes the decorator pattern.

As per the decorator pattern, you will implement toppings as decorators and pizzas will be decorated by those toppings' decorators. Practically each customer would want toppings of his desire and final bill-amount will be composed of the base pizzas and additionally ordered toppings. Each topping decorator would know about the pizzas that it is decorating and it's price. GetPrice() method of Topping object would return cumulative price of both pizza and the topping.

EDIT

Here's a code-example of explanation above.

public abstract class BasePizza
{
    protected double myPrice;

    public virtual double GetPrice()
    {
        return this.myPrice;
    }
}

public abstract class ToppingsDecorator : BasePizza
{
    protected BasePizza pizza;
    public ToppingsDecorator(BasePizza pizzaToDecorate)
    {
        this.pizza = pizzaToDecorate;
    }

    public override double GetPrice()
    {
        return (this.pizza.GetPrice() + this.myPrice);
    }
}

class Program
{
    [STAThread]
    static void Main()
    {
        //Client-code
        Margherita pizza = new Margherita();
        Console.WriteLine("Plain Margherita: " + pizza.GetPrice().ToString());

        ExtraCheeseTopping moreCheese = new ExtraCheeseTopping(pizza);
        ExtraCheeseTopping someMoreCheese = new ExtraCheeseTopping(moreCheese);
        Console.WriteLine("Plain Margherita with double extra cheese: " + someMoreCheese.GetPrice().ToString());

        MushroomTopping moreMushroom = new MushroomTopping(someMoreCheese);
        Console.WriteLine("Plain Margherita with double extra cheese with mushroom: " + moreMushroom.GetPrice().ToString());

        JalapenoTopping moreJalapeno = new JalapenoTopping(moreMushroom);
        Console.WriteLine("Plain Margherita with double extra cheese with mushroom with Jalapeno: " + moreJalapeno.GetPrice().ToString());

        Console.ReadLine();
    }
}

public class Margherita : BasePizza
{
    public Margherita()
    {
        this.myPrice = 6.99;
    }
}

public class Gourmet : BasePizza
{
    public Gourmet()
    {
        this.myPrice = 7.49;
    }
}

public class ExtraCheeseTopping : ToppingsDecorator
{
    public ExtraCheeseTopping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 0.99;
    }
}

public class MushroomTopping : ToppingsDecorator
{
    public MushroomTopping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 1.49;
    }
}

public class JalapenoTopping : ToppingsDecorator
{
    public JalapenoTopping(BasePizza pizzaToDecorate)
        : base(pizzaToDecorate)
    {
        this.myPrice = 1.49;
    }
}

这篇关于请帮我理解“装饰图案”以真实的世界为例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:22