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();
        }
    }
    

    总结

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

  • 相关阅读:
    Gevent高并发网络库精解
    python多线程参考文章
    python多线程
    进程与线程
    golang 微服务以及相关web框架
    微服务实战:从架构到发布
    python 常用库收集
    总结数据科学家常用的Python库
    20个最有用的Python数据科学库
    自然语言处理的发展历程
  • 原文地址:https://www.cnblogs.com/hippieZhou/p/9979019.html
Copyright © 2011-2022 走看看