zoukankan      html  css  js  c++  java
  • 设计模式系列

    装饰器模式允许向现有对象中添加新功能,同时又不改变其结构。

    介绍

    装饰器模式属于结构型模式,主要功能是能够动态地为一个对象添加额外功能。在保证现有功能的基础上,再添加新功能,可联想到 WPF 中的附件属性。

    类图描述

    由上图可知,我们定义了一个基础接口 IShape 用于约定对象的基础行为。然后通过定义ShapeDecorator 类 来扩展功能,RedShapleDecorator 为具体的扩展实现。

    代码实现

    1、定义接口

    public interface IShape
    {
        void Draw();
    }
    

    2、定义对象类型

    public class Circle:IShape
    {
        public void Draw()
        {
            Console.WriteLine("Shape:Circle");
        }
    }
    
    public class Rectangle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Shape:Rectangle");
        }
    }
    

    3、定义新的扩展装饰类

    public class ShapeDecorator:IShape
    {
        protected IShape decoratedShape;
    
        public ShapeDecorator(IShape decoratedShape)
        {
            this.decoratedShape = decoratedShape;
        }
    
        public virtual void Draw()
        {
            decoratedShape.Draw();
        }
    }
    

    4、定义扩展类的具体实现

    public class RedShapleDecorator : ShapeDecorator
    {
        public RedShapleDecorator(IShape decoratedShape) : base(decoratedShape)
        {
        }
    
        public override void Draw()
        {
            this.decoratedShape.Draw();
            setRedBorder(this.decoratedShape);
        }
    
        private void setRedBorder(IShape decoratedShape)
        {
            Console.WriteLine("Border Color:Red");
        }
    }
    

    5、上层调用

    class Program
    {
        static void Main(string[] args)
        {
            IShape circle = new Circle();
            IShape redCircle = new RedShapleDecorator(new Circle());
    
            IShape redRectangle = new RedShapleDecorator(new Rectangle());
            Console.WriteLine("Circle with normal border");
            circle.Draw();
    
            Console.WriteLine("Circle of red border");
            redCircle.Draw();
    
            Console.WriteLine("Rectangel of red border");
            redRectangle.Draw();
    
            Console.ReadKey();
        }
    }
    

    总结

    装饰器模式使得对象的核心功能和扩展功能能够各自独立扩展互不影响。但是对于装饰功能较多的情况下不建议采用这种模式。

  • 相关阅读:
    非Ajax实现客户端的异步调用
    转:IE7远程客户端本地预览图片的解决办法(兼容IE6)
    How to Persist With Your Goals When the Going Gets Tough 编程爱好者
    How to Persist When You Really Want to Quit 编程爱好者
    JAVA环境变量设置
    手机连接WIFI总掉线的解决方法
    servlet中web.xml配置详解(转)
    ArrayList的contains方法[转]
    eclipse下 alt+/快捷键方式失效的解决
    关于数据结构,关于算法,关于hash桶算法
  • 原文地址:https://www.cnblogs.com/hippieZhou/p/9979019.html
Copyright © 2011-2022 走看看