设计模式-装饰模式

Decorator Pattern

装饰模式:装饰模式主要是解决了,我们以动态的,透明的形式为某个类型添加一个新的职责,客户程序可以不知道我们具体添加的功能职责,

而客户程序只是根据对象提供的方法进行调用即可。而具体职责操作留给装饰对象去完成。

 

 

复制代码
  abstract class Component
    {
        public abstract void Operation();
    }

    class ConcreteComponent : Component
    {
        public override void Operation()
        {
            Console.WriteLine("具体对象的操作");
        }
    }

    abstract class Decorator : Component
    {
        protected Component component;

        public void SetComponent(Component component)
        {
            this.component = component;
        }

        public override void Operation()
        {
            if (component != null)
            {
                component.Operation();
            }
        }
    }

    class ConcreteDecoratorA : Decorator
    {
        private string addedState;

        public override void Operation()
        {
            base.Operation();
            addedState = "New State";
            Console.WriteLine("具体装饰对象A的操作");
        }
    }

    class ConcreteDecoratorB : Decorator
    {

        public override void Operation()
        {
            base.Operation();
            AddedBehavior();
            Console.WriteLine("具体装饰对象B的操作");
        }

        private void AddedBehavior()
        {

        }
    }
复制代码

 

客户端调用:

复制代码
ConcreteComponent c = new ConcreteComponent();
ConcreteDecoratorA d1 = new ConcreteDecoratorA();
ConcreteDecoratorB d2 = new ConcreteDecoratorB();

d1.SetComponent(c);
d2.SetComponent(d1);
d2.Operation();
复制代码

 

学习示例: 
复制代码
    class Person
    {
        public Person()
        { }

        private string name;
        public Person(string name)
        {
            this.name = name;
        }

        public virtual void Show()
        {
            Console.WriteLine("装扮的{0}", name);
        }
    }

    class Finery : Person
    {
        protected Person component;

        //打扮
        public void Decorate(Person component)
        {
            this.component = component;
        }

        public override void Show()
        {
            if (component != null)
            {
                component.Show();
            }
        }
    }


    class TShirts : Finery
    {
        public override void Show()
        {
            Console.Write("大T恤 ");
            base.Show();
        }
    }

    class BigTrouser : Finery
    {
        public override void Show()
        {
            Console.Write("垮裤 ");
            base.Show();
        }
    }

    class Sneakers : Finery
    {
        public override void Show()
        {
            Console.Write("破球鞋 ");
            base.Show();
        }
    }

    class Suit : Finery
    {
        public override void Show()
        {
            Console.Write("西装 ");
            base.Show();
        }
    }

    class Tie : Finery
    {
        public override void Show()
        {
            Console.Write("领带 ");
            base.Show();
        }
    }

    class LeatherShoes : Finery
    {
        public override void Show()
        {
            Console.Write("皮鞋 ");
            base.Show();
        }
    }
复制代码

客户端调用:

复制代码
 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();
复制代码

 

http://kb.cnblogs.com/page/84510/ 

大话设计模式-程杰

posted on   youhui  阅读(189)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示