Design Patterns: Decorator Pattern

Decorator Pattern dynamically responsibility attached to the object. To expand the capabilities decorated to provide a more flexible than inheritance alternative.

1. Role

  • Abstract component (Component) Role: the original object to be packaged, it is an abstract class or interface.
  • DETAILED member (ConcreteComponent) Role: Final actual object to be decorated, it is Component implementation class.
  • Decoration (Decorator) role: is an abstract class that inherits from Component, also holds a reference to the Component instance of an object.
  • Specific decoration (ConcreteComponent) Role: concrete decorator objects, Decorator implementation class is responsible for ConcreteComponent additional responsibilities.

2. Demo background

  • Each tea a tea shop can choose to add some of his materials such as oatmeal, pudding, red beans, pearl and so on. Adding different materials prices may be different, or is it that we all want to, and add a variety of accessories, so when we design objects encountered difficulties, we going to permutations and combinations to create a subclass of it? This is clearly not good, the number of class explosions, rigid design. At this time we can refer to the decorator pattern, instead of a combination of inheritance.

3. code implementation

  • Abstract component class (Component): MilkyTea
public abstract class MilkyTea(奶茶)
    {
        public string description;
        public abstract double GetFee();
        //与java不一样,java父类方法不需要加关键词Virtual就可以在子类中重写
        public virtual string GetDescription()
        {
            return description;
        }
    }
  • Construction of concrete classes (ConcreteComponent): MilkGreenTea (milk green)
public class MilkGreenTea : MilkyTea
    {
        public MilkGreenTea()
        {
            description = "Milk Green Tea";
        }
        public override double GetFee()
        {
            return 10;
        }
    }
  • Decoration (Decorator): CondimentDecorator (accessories)
public abstract class CondimentDecorator : MilkyTea
    {
        public MilkyTea milkyTea;
    }
  • Specific decoration (ConcreteComponent): Oats (Oats), Pudding (Pudding)
    public class Oats : CondimentDecorator
    {
        public Oats(MilkyTea milkyTea)
        {
            this.milkyTea = milkyTea;
        }
        public override double GetFee()
        {
            return 1 + milkyTea.GetFee();
        }
        public override string GetDescription()
        {
            return milkyTea.GetDescription() + ",Oats";
        }
    }
    public class Pudding : CondimentDecorator
    {
        public Pudding(MilkyTea milkyTea)
        {
            this.milkyTea = milkyTea;
        }
        public override double GetFee()
        {
            return 3 + milkyTea.GetFee();
        }
        public override string GetDescription()
        {
            return milkyTea.GetDescription() + ",Pudding";
        }
    }
  • Test code and operating results
static void Main(string[] args)
        {
            MilkyTea tea = new MilkGreenTea();
            Console.WriteLine($"{tea.GetDescription()}:¥{tea.GetFee()}。");
            tea = new Pudding(tea);
            Console.WriteLine($"{((Pudding)tea).GetDescription()}:¥{tea.GetFee()}。");
            tea = new Oats(tea);
            Console.WriteLine($"{tea.GetDescription()}:¥{tea.GetFee()}。");
        }

operation result

4. advantages and disadvantages

  • Advantages: injecting an elastic design. Meet the principle of open and close, easy to expand.
  • Indeed the point: in the design will add a lot of categories.

5. Source Address

https://github.com/DonyGu/DesignPatterns

Guess you like

Origin www.cnblogs.com/donyblog/p/11392037.html