分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Participants

The classes and/or objects participating in this pattern are:

  • Component (LibraryItem)

    • Defines the interface for objects that can have responsibilities added to them dynamically.
  • ConcreteComponent (Book, Video)
    • Defines an object to which additional responsibilities can be attached.
  • Decorator (Decorator)
    • Maintains a reference to a Component object and defines an interface that conforms to Component's interface.
  • ConcreteDecorator (Borrowable)
    • Adds responsibilities to the component.

Sample Code in C#


This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Decorator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    /// <summary>    /// Startup class for Structural Decorator Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Create ConcreteComponent and two Decorators            var c = new ConcreteComponent();            var d1 = new ConcreteDecoratorA();            var d2 = new ConcreteDecoratorB();            // Link decorators            d1.SetComponent(c);            d2.SetComponent(d1);            d2.Operation();        }        #endregion    }    /// <summary>    /// The 'Component' abstract class    /// </summary>    internal abstract class Component    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public abstract void Operation();        #endregion    }    /// <summary>    /// The 'ConcreteComponent' class    /// </summary>    internal class ConcreteComponent : Component    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            Console.WriteLine("ConcreteComponent.Operation()");        }        #endregion    }    /// <summary>    /// The 'Decorator' abstract class    /// </summary>    internal abstract class Decorator : Component    {        #region Fields        /// <summary>        /// The component.        /// </summary>        protected Component Component;        #endregion        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            if (this.Component != null)            {                this.Component.Operation();            }        }        /// <summary>        /// The set component.        /// </summary>        /// <param name="component">        /// The component.        /// </param>        public void SetComponent(Component component)        {            this.Component = component;        }        #endregion    }    /// <summary>    /// The 'ConcreteDecoratorA' class    /// </summary>    internal class ConcreteDecoratorA : Decorator    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            base.Operation();            Console.WriteLine("ConcreteDecoratorA.Operation()");        }        #endregion    }    /// <summary>    /// The 'ConcreteDecoratorB' class    /// </summary>    internal class ConcreteDecoratorB : Decorator    {        #region Public Methods and Operators        /// <summary>        /// The operation.        /// </summary>        public override void Operation()        {            base.Operation();            this.AddedBehavior();            Console.WriteLine("ConcreteDecoratorB.Operation()");        }        #endregion        #region Methods        /// <summary>        /// The added behavior.        /// </summary>        private void AddedBehavior()        {        }        #endregion    }}// Output:/*ConcreteComponent.Operation()ConcreteDecoratorA.Operation()ConcreteDecoratorB.Operation()*/

This real-world code demonstrates the Decorator pattern in which 'borrowable' functionality is added to existing library items (books and videos).

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Decorator Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// Startup class for Real-World Decorator Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Create book            var book = new Book("Worley", "Inside ASP.NET", 10);            book.Display();            // Create video            var video = new Video("Spielberg", "Jaws", 23, 92);            video.Display();            // Make video borrowable, then borrow and display            Console.WriteLine("\nMaking video borrowable:");            var borrowvideo = new Borrowable(video);            borrowvideo.BorrowItem("Customer #1");            borrowvideo.BorrowItem("Customer #2");            borrowvideo.Display();        }        #endregion    }    /// <summary>    /// The 'Component' abstract class    /// </summary>    internal abstract class LibraryItem    {        // Property        #region Public Properties        /// <summary>        /// Gets or sets the number of copies.        /// </summary>        public int NumCopies { get; set; }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public abstract void Display();        #endregion    }    /// <summary>    /// The 'ConcreteComponent' class    /// </summary>    internal class Book : LibraryItem    {        #region Fields        /// <summary>        /// The author.        /// </summary>        private string author;        /// <summary>        /// The title.        /// </summary>        private string title;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Book"/> class.        /// </summary>        /// <param name="author">        /// The author.        /// </param>        /// <param name="title">        /// The title.        /// </param>        /// <param name="numCopies">        /// The number of copies.        /// </param>        public Book(string author, string title, int numCopies)        {            this.author = author;            this.title = title;            this.NumCopies = numCopies;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public override void Display()        {            Console.WriteLine("\nBook ------ ");            Console.WriteLine(" Author: {0}", this.author);            Console.WriteLine(" Title: {0}", this.title);            Console.WriteLine(" # Copies: {0}", this.NumCopies);        }        #endregion    }    /// <summary>    /// The 'ConcreteComponent' class    /// </summary>    internal class Video : LibraryItem    {        #region Fields        /// <summary>        /// The director.        /// </summary>        private string director;        /// <summary>        /// The play time.        /// </summary>        private int playTime;        /// <summary>        /// The title.        /// </summary>        private string title;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Video"/> class.        /// </summary>        /// <param name="director">        /// The director.        /// </param>        /// <param name="title">        /// The title.        /// </param>        /// <param name="numCopies">        /// The number of copies.        /// </param>        /// <param name="playTime">        /// The play time.        /// </param>        public Video(string director, string title, int numCopies, int playTime)        {            this.director = director;            this.title = title;            this.NumCopies = numCopies;            this.playTime = playTime;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public override void Display()        {            Console.WriteLine("\nVideo ----- ");            Console.WriteLine(" Director: {0}", this.director);            Console.WriteLine(" Title: {0}", this.title);            Console.WriteLine(" # Copies: {0}", this.NumCopies);            Console.WriteLine(" Playtime: {0}\n", this.playTime);        }        #endregion    }    /// <summary>    /// The 'Decorator' abstract class    /// </summary>    internal abstract class Decorator : LibraryItem    {        #region Fields        /// <summary>        /// The library item.        /// </summary>        protected readonly LibraryItem LibraryItem;        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Decorator"/> class.        /// </summary>        /// <param name="libraryItem">        /// The library item.        /// </param>        protected Decorator(LibraryItem libraryItem)        {            this.LibraryItem = libraryItem;        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The display.        /// </summary>        public override void Display()        {            this.LibraryItem.Display();        }        #endregion    }    /// <summary>    /// The 'ConcreteDecorator' class    /// </summary>    internal class Borrowable : Decorator    {        #region Fields        /// <summary>        /// The borrowers.        /// </summary>        protected readonly List<string> Borrowers = new List<string>();        #endregion        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Borrowable"/> class.        /// </summary>        /// <param name="libraryItem">        /// The library item.        /// </param>        public Borrowable(LibraryItem libraryItem)            : base(libraryItem)        {        }        #endregion        #region Public Methods and Operators        /// <summary>        /// The borrow item.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public void BorrowItem(string name)        {            this.Borrowers.Add(name);            this.LibraryItem.NumCopies--;        }        /// <summary>        /// The display.        /// </summary>        public override void Display()        {            base.Display();            foreach (string borrower in this.Borrowers)            {                Console.WriteLine(" borrower: " + borrower);            }        }        /// <summary>        /// The return item.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        public void ReturnItem(string name)        {            this.Borrowers.Remove(name);            this.LibraryItem.NumCopies++;        }        #endregion    }}// Output:/*Book ------ Author: Worley Title: Inside ASP.NET # Copies: 10Video ----- Director: Spielberg Title: Jaws # Copies: 23 Playtime: 92Making video borrowable:Video ----- Director: Spielberg Title: Jaws # Copies: 21 Playtime: 92 borrower: Customer #1 borrower: Customer #2*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Decorator(C#)相关推荐

  1. Design Pattern - Visitor(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Memento(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Flyweight(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. h2 迁移到 mysql_[saiku] 将saiku自带的H2嵌入式数据库迁移到本地mysql数据库
  2. python中零碎的一点东西
  3. Zoe Liu:被Chrome Media团队的专注精神感染
  4. 如何查看header object和category 03的IBASE的relationship关系
  5. mysql字符集问题_mysql字符集问题
  6. Python应用实战-Pandas 计算连续行为天数的几种思路
  7. webpack打包后的文件夹是空的_webpack打包Vue工程
  8. linux系统内核参数命令,Linux内核启动参数解析及添加
  9. java工厂到接口_Java基础——接口简单工厂
  10. SpringBoot mysql房屋租赁系统4.0 租房系统源码(包远程安装
  11. 怎么把半角引号替换成全角_巧妙批量互换全角与半角双引号
  12. Oracle中关于临时表空间无法释放问题
  13. 网上跳蚤市场网站系统HTML5+Vue+nodejs
  14. 剖析SQL Server 2005查询通知之基础篇
  15. 程序员学完深入理解计算机系统,深入理解计算机系统9个重点笔记
  16. 一款IM即时通讯聊天系统源码,包含app和后台源码
  17. JSP学习笔记之基础教程
  18. margin collapsing现象
  19. 一键恢复windows11经典右键菜单
  20. 基于Android的智能浇花控制系统设计

热门文章

  1. ERROR 1044 (42000)报错的解决
  2. 把jpg转换成pdf软件
  3. Wcf 基础教程 服务寄宿之 Windows 服务寄宿
  4. 在ASP.NET Core应用程序中使用分布式缓存
  5. docker logstash log docker logs to elasticsearch
  6. Using POI to replace elements in WORD(.docx/.doc)(使用POI替换word中的特定字符/文字)【改进】...
  7. linux sshd cpu 过高 问题解决
  8. linux shell head tail 用法简介
  9. java maven mvn clean package 打包执行流程
  10. java反序列化漏洞的一些gadget