public void Test() { Tank t = new Tank50(); Decorator d = new Decorator(t); d.Shot(); d.Run(); Decorator da = new DecoratorA(d); da.Shot(); da.Run(); Decorator db = new DecoratorB(da); db.Shot(); db.Run(); Decorator dc = new DecoratorC(db); dc.Shot(); dc.Run(); Console.ReadLine(); }
public static void Main(string[] args) { #region Prototype Component component = new ConcreteComponent(); Decorator decorator = new DecoratorA(); decorator.SetComponent(component); decorator.Operation(); Decorator decorator1 = new DecoratorB(); decorator1.SetComponent(decorator); decorator1.Operation(); #endregion Prototype var person = new Person("小明"); Console.WriteLine("\n第一种装扮:\n"); var pants = new Pants(); pants.Decorate(person); pants.Show(); Console.WriteLine("\n第二种装扮:\n"); var shirts = new Tshirts(); shirts.Decorate(pants); shirts.Show(); Console.ReadLine(); }
static void Main(string[] args) { ComponentA componentA = new ComponentA(); DecoratorA decoratorA = new DecoratorA(); DecoratorB decoratorB = new DecoratorB(); decoratorA.SetComponent(componentA); decoratorB.SetComponent(decoratorA); decoratorB.Show(); }
/* * 装饰者模式: * Component:定义ConcreteComponent和Decorator类要实现的方法,简单来说如果一个类继承于该类就具有装饰或被装饰能力。 * ConcreteComponent:让Decorator对象为自己添加功能。有时候使用ConcreteComponent的派生类提供核心功能,在这种情况就是用ConcreteComponent替代了Component的功能,而且装饰者是继承于ConcreteComponent的子类。 * Decorator:具有特定装饰功能的类,用来装饰ConcreteComponent类。 */ static void Main(string[] args) { Component component = new ConcreteComponent(); component = new DecoratorA(component); component = new DecoratorB(component); component.Show(); System.Console.ReadKey(); }
static void Main(string[] args) { IComponent orgComponent = new Component(); DecoratorA aComponent = new DecoratorA(orgComponent); DecoratorB bComponent = new DecoratorB(orgComponent); DecoratorA abComponent = new DecoratorA(bComponent); Console.WriteLine($"Original object: {orgComponent.Operation()}"); Console.WriteLine($"A Decorator object: {aComponent.Operation()}. {aComponent.AddedBehavior()}"); Console.WriteLine($"B Decorator object: {bComponent.Operation()}"); Console.WriteLine($"AB Decorator object: {abComponent.Operation()}"); Console.ReadKey(); }