当前位置:网站首页 / WPF / 正文

[AYJS] wpf设计模式学习[2]-装饰者模式

时间:2015年07月07日 | 作者 : aaronyang | 分类 : WPF | 浏览: 2357次 | 评论 0

装饰者模式

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰者模式比生成子类更为灵活。

Image 8.png

示例代码:控制台程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhuangShi
{
    public abstract class Component
    {
        public abstract void Operation();

    }

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


    }


    public abstract class Decorator : Component
    {
        protected Component component;
        public void SetComponent(Component component)
        {
            this.component = component;
        }
        public override void Operation()
        {
            if (component != null)
            {
                component.Operation();
            }
        }

    }

    public class ConcreteComponentA : Decorator
    {
        private string addedState;
        public override void Operation()
        {
            base.Operation();
            addedState = "新状态";
            Console.WriteLine("对象A的操作");
        }


     
    }


    public class ConcreteComponentB : Decorator
    {
  
        public override void Operation()
        {
            base.Operation();
            AddedBehavior();
            Console.WriteLine("对象B的操作");
        }
        public void AddedBehavior() { 
        
        }


    }





}

然后 控制台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZhuangShi
{
    class Program
    {
        static void Main(string[] args)
        {
            ConcreteComponent c = new ConcreteComponent();
            ConcreteComponentA d1 = new ConcreteComponentA();
            ConcreteComponentB d2 = new ConcreteComponentB();
            d1.SetComponent(c);
            d2.SetComponent(d1);
            d2.Operation();

            Console.ReadLine();
        }
    }
}

效果:

    具体的操作对象

    对象A的操作

    对象B的操作

最顶端  抽象父类 有个公共方法,好比wpf的 OnRender方法,你可以render的时候 添加任何操作,而父类无需知道自己下面有多少个实现了的 OnRender方法。

当系统需要更新功能的时候,是向旧的类中添加新的代码。这些新的代码通常装饰了原有类的核心职责或主要行为。

那么接下来,我自己来模拟下WPF的 装饰者的DEMO,让你更加了解下装饰者,增加OnRender方法(比如给水果染色)

新建一个 UIElement类- (比如 水果)

    public abstract class UIElement
    {
        public abstract void OnRender();
    }

设置装饰器-装饰工具类 (比如喷色彩的枪)

    public abstract class DecoratorBase : UIElement
    {
        public UIElement element;
        public void Decorate(UIElement element)
        {
            this.element = element;
        }

        public override void OnRender()
        {
            if (element != null)
            {
                element.OnRender();
            }
        }
    }

创建其他装饰器-具体怎么装饰

  public class RedShadowDecorator : DecoratorBase
    {
        public override void OnRender()
        {
            base.OnRender();
            Console.WriteLine("增加红色的阴影");
        }
    }


    public class BlueShadowDecorator : DecoratorBase
    {
        public override void OnRender()
        {
            base.OnRender();
            Console.WriteLine("增加蓝色的阴影");
        }
    }


    public class BlackShadowDecorator : DecoratorBase
    {
        public override void OnRender()
        {
            base.OnRender();
            Console.WriteLine("增加黑色的阴影");
        }
    }

Image 13.png


OK,到这里我们也介绍完了,下次我讲解下WPF的 装饰器应用

推荐您阅读更多有关于“WPF4.5设计模式,”的文章

猜你喜欢

额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

  查看权限

抖音:wpfui 工作wpf,目前主maui

招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰

目前在合肥市企迈科技就职

AYUI8全源码 Github地址:前往获取

杨洋(AaronYang简称AY,安徽六安人)AY唯一QQ:875556003和AY交流

高中学历,2010年开始web开发,2015年1月17日开始学习WPF

声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费

不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo

查看捐赠

AYUI7.X MVC教程 更新如下:

第一课 第二课 程序加密教程

标签列表