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(); }
public static void Main() { ConcreteComponent comp = new ConcreteComponent(); Decorator deco = new ConcreteDecorator1(new ConcreteDecorator2(comp)); deco.Operation(); }
static void Main(string[] args) { IComponent component = new ConcreteComponent(); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB.Operation(); }
static void Main(string[] args) { ConcreteComponent concreteComponent = new ConcreteComponent(); //实例化两个装饰器 ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(); ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(); //对concreteComponent对象进行装饰 concreteDecoratorA.SetComponent(concreteComponent); concreteDecoratorB.SetComponent(concreteComponent); //显示装饰后的结果 Console.WriteLine("经过装饰器A装饰后的操作:"); concreteDecoratorA.Operation(); Console.WriteLine("经过装饰器B装饰后的操作:"); concreteDecoratorB.Operation(); //演员与化妆师举例 Actor actor = new Actor(); ModernStyList modernStylist = new ModernStyList(); AncientStylist ancientStylist = new AncientStylist(); //给演员进行现代剧化妆 modernStylist.MakeUp(actor); modernStylist.Act(); //给演员进行古装剧化妆 ancientStylist.MakeUp(actor); ancientStylist.Act(); }
static void Main(string[] args) { IComponent concreteComponent = new ConcreteComponent(); IComponent component = new ConcreteDecoratorA(new ConcreteDecoratorB(concreteComponent)); component.Operation(); Console.ReadKey(); }
static void Main(string[] args) { Component component = new ConcreteComponent(); ConcreteDecorator concreteDecorator = new ConcreteDecorator(component); concreteDecorator.Operation(); Console.ReadKey(); }
static void Main(string[] args) { IComponent component = new ConcreteComponent(); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB.Operation(); }
/* * 装饰者模式: * 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(); }
public void Run() { ConcreteComponent concreteComponent = new ConcreteComponent(); ConcreteImplA concreteImplA = new ConcreteImplA(); ConcreteImplB concreteImplB = new ConcreteImplB(); concreteImplA.SetComponent(concreteComponent); concreteImplB.SetComponent(concreteImplA); concreteImplB.Operation(); }
static void Main1(string[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Console.ReadKey(); }
static void Main(string[] args) { ConcreteComponent component = new ConcreteComponent(); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(); decoratorA.Component = component; decoratorB.Component = decoratorA; decoratorB.Operation(); Console.Read(); }
/// <summary> /// The Decorator Design Pattern aims to add functionality to existing classes. This is done by implementing a decorator base-class, that holds the classes to be decorated and executes its methods, thus enabling a chain of calls. /// </summary> /// <example> /// Real life uses include... /// - adding filters-criterias to filter a data-set /// - adding calculations to models - fx meteological maps /// - decorating a command with others, thus creating a chain of commands to be executed /// </example> private static void Main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(c); ConcreteDecoratorB d2 = new ConcreteDecoratorB(d1); d2.Operation(); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { ConcreteComponent cComponent = new ConcreteComponent(); ConcreteDecoratorA cdA = new ConcreteDecoratorA(); ConcreteDecoratorB cdB = new ConcreteDecoratorB(); cdA.SetComponent(cComponent); cdB.SetComponent(cdA); cdB.Operation(); Console.ReadLine(); }
static void Main(string[] args) { ConcreteComponent C = new ConcreteComponent(); ConcreteDeconratorA deconratorA = new ConcreteDeconratorA(); ConcreteDeconratorB deconratorB = new ConcreteDeconratorB(); // 装饰的方法是:首先用ConcreteComponent实例化对象然后分别实例化deconratorA,deconratorB对象deconratorA来包装C,在用deconratorB来包装deconratorA,最终执行deconratorB的Operation。 deconratorA.SetComponent(C); deconratorB.SetComponent(deconratorA); deconratorB.Operation(); Console.Read(); }
static void Main(string[] args) { ConcreteComponent concreteComponent = new ConcreteComponent(); ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(); ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(); concreteDecoratorA.Component = concreteComponent; concreteDecoratorB.Component = concreteDecoratorA; concreteDecoratorB.Operation(); Console.Read(); }
static void Main(string[] args) { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); }
static void Main(string[] args) { Console.WriteLine("***Decorator pattern Demo***\n"); ConcreteComponent cc = new ConcreteComponent(); ConcreteDecoratorEx1 decorator1 = new ConcreteDecoratorEx1(); decorator1.SetTheComponent(cc); decorator1.MakeHouse(); ConcreteDecoratorEx2 decorator2 = new ConcreteDecoratorEx2(); //Adding results from decorator1 decorator2.SetTheComponent(decorator1); decorator2.MakeHouse(); Console.ReadKey(); }
static void Main(string[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(); ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(); ConcreteDecoratorC concreteDecoratorC = new ConcreteDecoratorC(); concreteDecoratorA.SetComponent(c); concreteDecoratorB.SetComponent(concreteDecoratorA); concreteDecoratorC.SetComponent(concreteDecoratorB); concreteDecoratorC.Operation(); Console.ReadKey(); }
private static void Main(string[] args) { var c = new ConcreteComponent(); var a = new ConcreteDecoratorA(); var b = new ConcreteDecoratorB(); c.Operation(); a.SetComponent(c); a.Operation(); b.SetComponent(a); b.Operation(); Console.ReadKey(); }
static void Main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); // Wait for user Console.Read(); }
private static void Main(string[] args) { var c = new ConcreteComponent(); var a = new ConcreteDecoratorA(); var b = new ConcreteDecoratorB(); c.Operation(); a.SetComponent(c); a.Operation(); b.SetComponent(a); b.Operation(); Console.ReadKey(); }
static void Main() { // Create ConcreteComponent and two Decorators ConcreteComponent component = new ConcreteComponent(); ConcreteDecoratorA decorator1 = new ConcreteDecoratorA(); ConcreteDecoratorB decorator2 = new ConcreteDecoratorB(); // Link decorators decorator1.SetComponent(component); decorator2.SetComponent(decorator1); decorator2.Operation(); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***Decorator pattern Demo***\n"); ConcreteComponent cc = new ConcreteComponent(); ConcreteDecoratorEx1 decoratorEx1 = new ConcreteDecoratorEx1(); decoratorEx1.SetTheComponent(cc); decoratorEx1.makeHouse(); ConcreteDecoratorEx2 decoratorEx2 = new ConcreteDecoratorEx2(); decoratorEx2.SetTheComponent(decoratorEx1); decoratorEx2.makeHouse(); Console.ReadKey(); }
private static void Main() { Console.WriteLine("***Decorator pattern***\n"); ConcreteComponent cc = new ConcreteComponent(); ConcreteDecoratorEx1 dc1 = new ConcreteDecoratorEx1(); dc1.SetTheComponent(cc); dc1.MakeHouse(); ConcreteDecoratorEx2 dc2 = new ConcreteDecoratorEx2(); dc2.SetTheComponent(dc1); dc2.MakeHouse(); Console.ReadLine(); }
static void Main(string[] args) { try { {//BasicStructure ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } {//SituationSimulation //第一种写法 Console.WriteLine("***************第一种写法:***************"); AppleHandset handset = new AppleHandset(); HandsetSticker handsetSticker = new HandsetSticker(handset); HandsetCasing handsetCasing = new HandsetCasing(handsetSticker); handsetCasing.Operation(); //第二种写法 Console.WriteLine("***************第二种写法:***************"); Handset appleHandset = new AppleHandset(); appleHandset = new HandsetSticker(appleHandset); appleHandset = new HandsetCasing(appleHandset); appleHandset.Operation(); //第二种写法的简化 Console.WriteLine("***************第二种写法的简化:***************"); new HandsetCasing(new HandsetSticker(new AppleHandset())).Operation(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
public static void Main() { ConcreteComponent comp = new ConcreteComponent(); Decorator deco = new ConcreteDecorator1(new ConcreteDecorator2(comp)); deco.Operation(); }
static void Main(string[] args) { try { //介绍 // 意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。 //主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。 //何时使用:在不想增加很多子类的情况下扩展类。 //如何解决:将具体功能职责划分,同时继承装饰者模式。 //关键代码: 1、Component 类充当抽象角色,不应该具体实现。 2、修饰类引用和继承 Component 类,具体扩展类重写父类方法。 //应用实例: 1、孙悟空有 72 变,当他变成"庙宇"后,他的根本还是一只猴子,但是他又有了庙宇的功能。 2、不论一幅画有没有画框都可以挂在墙上,但是通常都是有画框的,并且实际上是画框被挂在墙上。在挂在墙上之前,画可以被蒙上玻璃,装到框子里;这时画、玻璃和画框形成了一个物体。 //优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。 //缺点:多层装饰比较复杂。 //使用场景: 1、扩展一个类的功能。 2、动态增加功能,动态撤销。 //注意事项:可代替继承。 //概念:1.动态地给一个对象添加一个额外的职责,就增加功能来说,装饰模式比生成子类更加灵活、 // 2.装饰器模式是利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中(DPE) #region 装饰模式 ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Person xc = new Person("小菜"); Console.WriteLine("\n第一种装扮:"); Sneakers pqx = new Sneakers(); BigTrouser kk = new BigTrouser(); TShirts dtx = new TShirts(); pqx.Decorate(xc); kk.Decorate(pqx); dtx.Decorate(kk); dtx.Show(); Console.WriteLine("\n第二种装扮:"); LeatherShoes px = new LeatherShoes(); Tie ld = new Tie(); Suit xz = new Suit(); px.Decorate(xc); ld.Decorate(px); xz.Decorate(ld); xz.Show(); Console.WriteLine("\n第三种装扮:"); Sneakers pqx2 = new Sneakers(); LeatherShoes px2 = new LeatherShoes(); BigTrouser kk2 = new BigTrouser(); Tie ld2 = new Tie(); pqx2.Decorate(xc); px2.Decorate(pqx); kk2.Decorate(px2); ld2.Decorate(kk2); ld2.Show(); #endregion } catch (Exception e) { Console.WriteLine($"遇到异常,ErrorMsg:{e.Message}"); Console.ReadLine(); } }