static void Main() { var component = new ConcreteComponent(); var decorator = new ConcreteDecorator(component); decorator.Operation(); }
//https://ru.wikipedia.org/wiki/Декоратор_(шаблон_проектирования) //https://refactoring.guru/ru/design-patterns/decorator //https://metanit.com/sharp/patterns/4.1.php static void Main(string[] args) { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA dA = new ConcreteDecoratorA(); ConcreteDecoratorB dB = new ConcreteDecoratorB(); // Link decorators dA.SetComponent(c); dB.SetComponent(dA); dA.Operation(); dB.Operation(); //OnInterface Implement implement = new Implement(); ConcreteDecoratorAI dAI = new ConcreteDecoratorAI(); ConcreteDecoratorBI dBI = new ConcreteDecoratorBI(); // Link decorators dAI.SetComponent(implement); dBI.SetComponent(dAI); implement.Operation(); dAI.Operation(); dBI.Operation(); // Wait for user Console.Read(); }
static void Main(string[] args) { var client = new Client(); // Клиент использует простой компонент var simple = new ConcreteComponent(); Console.WriteLine("Client. ConcreteComponent:"); client.Call(simple); Console.WriteLine(); // Декораторы могут обёртывать не только // простые компоненты, но и другие декораторы. var decorator1 = new ConcreteDecoratorA(simple); Console.WriteLine("Client. ConcreteComponent in DecoratorA:"); client.Call(decorator1); Console.WriteLine(); var decorator2 = new ConcreteDecoratorB(decorator1); Console.WriteLine("Client. ConcreteComponent in Decorators A and B:"); client.Call(decorator2); Console.WriteLine(); }
static void Main(string[] args) { Component component = new ConcreteComponent(); DecoratorClass decorator = new ConcreteDecoratorA(); decorator.SetComponent(component); decorator.Operation(); }
static void Main(string[] args) { Component component = new ConcreteComponent(); component = new ConcreteDecoratorA(component); component = new ConcreteDecoratorB(component); component.Operation(); }
static void Main(string[] args) { Component component = new ConcreteComponent(); Component decoratedComponent = new ConcreteDecoratorB(new ConcreteDecoratorA(component)); decoratedComponent.Operation(); Console.ReadLine(); }
static void Main(string[] args) { Component component = new ConcreteComponent(); Decorator decorA = new ConcreteDecoratorA(); Decorator decorB = new ConcreteDecoratorB(); decorA.component = component; decorB.component = decorA; decorB.Operation(); }
static void Main(string[] args) { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.setComponent(c); d2.setComponent(d1); d2.Operation(); }
static void Main(string[] args) { Component myComponent = new ConcreteComponent(); Console.WriteLine("CC: {0}", myComponent.Operation()); myComponent = new ConcreteDecoratorA(myComponent); Console.WriteLine("CC + DCA: {0}", myComponent.Operation()); myComponent = new ConcreteDecoratorB(myComponent); Console.WriteLine("CC + DCB: {0}", myComponent.Operation()); }
static void Main(string[] args) { var compo = new ConcreteComponent(); var decorator1 = new ConcreteDecoratorA(compo); var decorator2 = new ConcreteDecoratorB(compo); var decorator3 = new ConcreteDecoratorA(new ConcreteDecoratorB(compo)); compo.Operation(); decorator1.Operation(); decorator2.Operation(); decorator3.Operation(); }
private static void NewMethod2() { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); Console.Read(); }
static void Main(string[] args) { ConcreteComponent conr = new ConcreteComponent(); Component adec = new ADecorator(); Component bdec = new BDecorator(); (adec as ADecorator).SetComponent(conr); (bdec as BDecorator).SetComponent(adec); bdec.Operation(); Console.Read(); }
private static void Main() { var component = new ConcreteComponent(); var concreteDecoratorA = new ConcreteDecoratorA(); var concreteDecoratorB = new ConcreteDecoratorB(); concreteDecoratorA.SetComponent(component); concreteDecoratorB.SetComponent(concreteDecoratorA); concreteDecoratorB.Operation(); Console.ReadKey(); }
static void Main(string[] args) { var component = new ConcreteComponent(); component.Operation(); Console.WriteLine(); var changedComponent = new DecoratedComponent(component, "new state"); changedComponent.Operation(); Console.ReadKey(); }
static void Main() { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA dA = new ConcreteDecoratorA(); ConcreteDecoratorB dB = new ConcreteDecoratorB(); dA.SetComponent(c); dB.SetComponent(dA); dB.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(); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { Component obj = new ConcreteComponent(); obj.Operation(); Decorator objDecorator = new ConcreteDecorator(); objDecorator.SetComponent(obj); objDecorator.Operation(); Decorator objDecorator2 = new ConcreteDecorator2(); objDecorator2.SetComponent(objDecorator); objDecorator2.Operation(); }
static void Main() { ConcreteComponent cmp = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(cmp); d2.SetComponent(cmp); d1.Operation(); d2.Operation(); Console.ReadKey(); }
static void Main(string[] args) { Component component = new ConcreteComponent("Start"); Console.WriteLine(component.Operation()); component = new ConcreteDecoratorA(component); Console.WriteLine(component.Operation()); component = new ConcreteDecoratorB(component); Console.WriteLine(component.Operation()); Console.ReadLine(); }
/// <summary> /// The test first decorator. /// </summary> private static void TestFirstDecorator() { // 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.ReadKey(); }
static void Main(string[] args) { var client = new Client(); var simple = new ConcreteComponent(); System.Console.WriteLine("Client: I get a simple component"); client.ClientCode(simple); System.Console.WriteLine(); var decorator1 = new ConcreteDecoratorA(simple); var decorator2 = new ConcreteDecoratorB(decorator1); System.Console.WriteLine("Client: Now I've got a decorated component: "); client.ClientCode(decorator2); }
static void Main(string[] args) { Client client = new Client(); ConcreteComponent simpleComponent = new ConcreteComponent(); client.ClientCode(simpleComponent); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(simpleComponent); client.ClientCode(decoratorA); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA); client.ClientCode(decoratorB); }
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(); decorator2.SetTheComponent(decorator1); decorator2.MakeHouse(); }
static void Main(string[] args) { Console.WriteLine("***Decorator pattern Demo***"); ConcreteComponent cc = new ConcreteComponent(); ConcreteDecoratorEx_1 cd_1 = new ConcreteDecoratorEx_1(); cd_1.SetTheComponent(cc); cd_1.doJob(); ConcreteDecoratorEx_2 cd_2 = new ConcreteDecoratorEx_2(); cd_2.SetTheComponent(cd_1); cd_2.doJob(); Console.ReadLine(); }
/// <summary> /// Entry point into console application. /// </summary> 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.ReadKey(); }
{ /// <summary> /// Entry point into console application. /// </summary> static void Main() { // Create ConcreteComponent and two Decorators var c = new ConcreteComponent(); var d1 = new ConcreteDecoratorA(); var d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); // Wait for user Console.ReadKey(); }
static void Main() { 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() { ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA dA = new ConcreteDecoratorA(); ConcreteDecoratorB dB = new ConcreteDecoratorB(); dA.SetComponent(c); dB.SetComponent(dA); dA.Operation(); Console.WriteLine(); dB.Operation(); Console.Read(); }
static void Main(string[] args) { ConcreteComponent concreteComponent = new ConcreteComponent(); concreteComponent.Operation(); ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(); decoratorA.SetComponent(concreteComponent); ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(); decoratorB.SetComponent(concreteComponent); decoratorA.Operation(); decoratorB.Operation(); Console.WriteLine("-----------------------------"); Repo repo = new Repo(); repo.GetId(); DecoratorForIRepo decoratorForIRepo = new DecoratorForIRepo(repo); decoratorForIRepo.GetId(); Console.WriteLine("-----------------------------"); new CofeeHelper().ShouldSupportCondiments(); Console.WriteLine("-----------------------------"); BaseCar baseCar = new BaseCar(); Console.WriteLine(baseCar.GetCarCharacteristic()); Console.WriteLine(baseCar.GetCost()); ICarSet superCar = new AutomaticTransmissionSet(baseCar); Console.WriteLine(superCar.GetCarCharacteristic()); Console.WriteLine(superCar.GetCost()); }
static void Main(string[] args) { Client client = new Client(); var simple = new ConcreteComponent(); Console.WriteLine("Client: I get a simple component:"); client.ClientCode(simple); Console.WriteLine(); //.. as well as decorated ones. //Note how decorators can wrap only simple components but the other decorators as well. ConcreteDecoratorA decorator1 = new ConcreteDecoratorA(simple); ConcreteDecoratorB decorator2 = new ConcreteDecoratorB(decorator1); Console.WriteLine("Client: Now I've got a decorator component:"); client.ClientCode(decorator2); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); // Decorator ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // 先用ConcreteComponent实例化对象c // 然后用ConcreteComponentA的实例化对象d1包装c d1.SetComponent(c); // 再用ConcreteComponentB的实例化对象d2包装d1 d2.SetComponent(d1); //最终执行d2的Operation() d2.Operation(); Console.Read(); }
static void Main(string[] args) { // Se crea ConcreteComponent y 2 Decoradores ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Se liga los decoradores d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); // Espera al usuario Console.ReadKey(); }
public static void Run() { Console.WriteLine("This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object."); ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); /* * ConcreteComponent.Operation() * ConcreteDecoratorA.Operation() * ConcreteDecoratorB.Operation() */ }
static void Main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA dA = new ConcreteDecoratorA(); ConcreteDecoratorB dB = new ConcreteDecoratorB(); // Link decorators dA.SetComponent(c); dB.SetComponent(dA); dA.Operation(); Console.WriteLine(); dB.Operation(); // Wait for user Console.Read(); }
static void Main(string[] args) { #region 基本用法(对应Basic.cs) ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); #endregion #region 具体实例(对应Example.cs) Person xc = new Person("小菜"); Console.WriteLine("\n第一种装扮:"); Sneaker pqx = new Sneaker(); BigTrouser kk = new BigTrouser(); TShirt dtx = new TShirt(); 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.ReadLine(); #endregion }
static void Main(string[] args) { AbstractComponent ac = new ConcreteComponent(); AbstractComponent DecoratedAc = null; DecoratedAc = new Pre1Decorator(ac); DecoratedAc = new Pre2Decorator(DecoratedAc); DecoratedAc = new Post1Decorator(DecoratedAc); DecoratedAc.DoSomething(); DecoratedAc.DoSomethingElse(); DecoratedAc = new Post1Decorator(ac); DecoratedAc = new Pre2Decorator(DecoratedAc); DecoratedAc = new Pre1Decorator(DecoratedAc); DecoratedAc.DoSomething(); DecoratedAc.DoSomethingElse(); }