C#裝潢者形式實例剖析。本站提示廣大學習愛好者:(C#裝潢者形式實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是C#裝潢者形式實例剖析正文
本文實例講述了C#裝潢者形式。分享給年夜家供年夜家參考。詳細辦法以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 裝潢者 { class Program { static void Main(string[] args) { ConcreteComponent con = new ConcreteComponent(); ConcreteDecoratorA a = new ConcreteDecoratorA(); a.SetComponent(con); a.Operation(); a.AddBehavior(); Console.WriteLine("============================"); ConcreteDecoratorB B = new ConcreteDecoratorB(); B.SetComponent(a); a.AddBehavior(); B.Operation(); B.AddBehavior(); } public abstract class Component { public abstract void Operation(); } abstract class Decorator : Component { private Component component; public void SetComponent(Component component) { this.component = component; } public override void Operation() { if (component != null) { component.Operation(); } } } class ConcreteDecoratorA : Decorator { public override void Operation() { base.Operation(); } public void AddBehavior() { Console.WriteLine("裝潢A類的操作!"); } } class ConcreteDecoratorB : Decorator { public override void Operation() { base.Operation(); } public void AddBehavior() { Console.WriteLine("裝潢B類的操作!"); } } public class ConcreteComponent : Component { public override void Operation() { Console.WriteLine("詳細對象的操作!"); } } } }
願望本文所述對年夜家的C#法式設計有所贊助。