Home Decorator Pattern (데코레이터 패턴)
Post
Cancel

Decorator Pattern (데코레이터 패턴)

스타버즈

다이어그램

DecoratorDiagram

Beverage.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public abstract class Beverage
{
    public string Description = string.Empty;
    public enum Size { TALL, GRANDE,VENTI}
    public Size BevSize { get; set; } = Size.TALL;

    public virtual string GetDescription()
    {
        return Description;
    }

    public virtual double Cost()
    {
        double cost = 0;

        switch (BevSize)
        {
            case Size.TALL:
                cost = 0;
                break;
            case Size.GRANDE:
                cost = 0.15;
                break;
            case Size.VENTI:
                cost = 0.2;
                break;
            
            default:
                break;
        }
        return cost;
    }
}

Espresso.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Espresso : Beverage
{
    public Espresso()
    {
        Description = "에스프레소";
    }

    public override double Cost()
    {
        return base.Cost()+ 1.99;
    }

}

CondimentDecorator.cs

1
2
3
4
5
public abstract class CondimentDecorator : Beverage
{
    internal Beverage _beverage = null!;
    public abstract override string GetDescription();
}

Mocha.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Mocha : CondimentDecorator
{
    public Mocha(Beverage beverage)
    {
        _beverage = beverage;
    }

    public override double Cost()
    {
        return _beverage.Cost() + 0.20;
    }

    public override string GetDescription()
    {
        return _beverage.GetDescription() + ", 모카";
    }
}
This post is licensed under CC BY 4.0 by the author.