首页 > 代码库 > 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#装饰者模式实例代码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。