不太明白的Decorator Pattern

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.

有段很经典的代码:

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

namespace Decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            Video video = new Video("Spielberg", "Jaws", 23, 92);

            video.Display();

            // Make video borrowable, then borrow and display

            Console.WriteLine("\nMaking video borrowable:");

            Borrowable borrowvideo = new Borrowable(video);

            borrowvideo.BorrowItem("Customer #1");

            borrowvideo.BorrowItem("Customer #2");

            borrowvideo.Display();

            // Wait for user

            Console.ReadKey();


        }

        /// <summary>

        /// The 'Component' abstract class

        /// </summary>

        abstract class LibraryItem
        {

            private int _numCopies;

            // Property

            public int NumCopies
            {

                get { return _numCopies; }

                set { _numCopies = value; }

            }

            public abstract void Display();

        }

        /// <summary>

        /// The 'ConcreteComponent' class

        /// </summary>

        class Book : LibraryItem
        {

            private string _author;

            private string _title;

            // Constructor

            public Book(string author, string title, int numCopies)
            {

                this._author = author;

                this._title = title;

                this.NumCopies = numCopies;

            }

            public override void Display()
            {

                Console.WriteLine("\nBook ------ ");

                Console.WriteLine(" Author: {0}", _author);

                Console.WriteLine(" Title: {0}", _title);

                Console.WriteLine(" # Copies: {0}", NumCopies);

            }

        }

        /// <summary>

        /// The 'ConcreteComponent' class

        /// </summary>

        class Video : LibraryItem
        {

            private string _director;

            private string _title;

            private int _playTime;

            // Constructor

            public Video(string director, string title,

              int numCopies, int playTime)
            {

                this._director = director;

                this._title = title;

                this.NumCopies = numCopies;

                this._playTime = playTime;

            }

            public override void Display()
            {

                Console.WriteLine("\nVideo ----- ");

                Console.WriteLine(" Director: {0}", _director);

                Console.WriteLine(" Title: {0}", _title);

                Console.WriteLine(" # Copies: {0}", NumCopies);

                Console.WriteLine(" Playtime: {0}\n", _playTime);

            }

        }

        /// <summary>

        /// The 'Decorator' abstract class

        /// </summary>

        abstract class Decorator : LibraryItem
        {

            protected LibraryItem libraryItem;

            // Constructor

            public Decorator(LibraryItem libraryItem)
            {

                this.libraryItem = libraryItem;

            }

            public override void Display()
            {

                libraryItem.Display();

            }

        }

        /// <summary>

        /// The 'ConcreteDecorator' class

        /// </summary>

        class Borrowable : Decorator
        {

            protected List<string> borrowers = new List<string>();

            // Constructor

            public Borrowable(LibraryItem libraryItem)

                : base(libraryItem)
            {

            }

            public void BorrowItem(string name)
            {

                borrowers.Add(name);

                libraryItem.NumCopies--;

            }


            public void ReturnItem(string name)
            {

                borrowers.Remove(name);

                libraryItem.NumCopies++;

            }

            public override void Display()
            {

                base.Display();

                foreach (string borrower in borrowers)
                {

                    Console.WriteLine(" borrower: " + borrower);

                }

            }


        }
    }

}

现在我把Decorator   去掉了,运行结果依旧,还是很不理解Decorator   到底是做什么用途的,请高手赐教:

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

namespace Decorator
{
    class Program
    {
        static void Main(string[] args)
        {
            Book book = new Book("Worley", "Inside ASP.NET", 10);

            book.Display();

            // Create video

            Video video = new Video("Spielberg", "Jaws", 23, 92);

            video.Display();

            // Make video borrowable, then borrow and display

            Console.WriteLine("\nMaking video borrowable:");

            Borrowable borrowvideo = new Borrowable(video);

            borrowvideo.BorrowItem("Customer #1");

            borrowvideo.BorrowItem("Customer #2");

            borrowvideo.Display();

            // Wait for user

            Console.ReadKey();


        }

        /// <summary>

        /// The 'Component' abstract class

        /// </summary>

        abstract class LibraryItem
        {

            private int _numCopies;

            // Property

            public int NumCopies
            {

                get { return _numCopies; }

                set { _numCopies = value; }

            }

            public abstract void Display();

        }

        /// <summary>

        /// The 'ConcreteComponent' class

        /// </summary>

        class Book : LibraryItem
        {

            private string _author;

            private string _title;

            // Constructor

            public Book(string author, string title, int numCopies)
            {

                this._author = author;

                this._title = title;

                this.NumCopies = numCopies;

            }

            public override void Display()
            {

                Console.WriteLine("\nBook ------ ");

                Console.WriteLine(" Author: {0}", _author);

                Console.WriteLine(" Title: {0}", _title);

                Console.WriteLine(" # Copies: {0}", NumCopies);

            }

        }

        /// <summary>

        /// The 'ConcreteComponent' class

        /// </summary>

        class Video : LibraryItem
        {

            private string _director;

            private string _title;

            private int _playTime;

            // Constructor

            public Video(string director, string title,

              int numCopies, int playTime)
            {

                this._director = director;

                this._title = title;

                this.NumCopies = numCopies;

                this._playTime = playTime;

            }

            public override void Display()
            {

                Console.WriteLine("\nVideo ----- ");

                Console.WriteLine(" Director: {0}", _director);

                Console.WriteLine(" Title: {0}", _title);

                Console.WriteLine(" # Copies: {0}", NumCopies);

                Console.WriteLine(" Playtime: {0}\n", _playTime);

            }

        }


        class Borrowable : LibraryItem
        {

            protected List<string> borrowers = new List<string>();
            public LibraryItem LibraryItem;
            // Constructor

            public Borrowable(LibraryItem libraryItem)
            {
                LibraryItem = libraryItem;
            }

            public void BorrowItem(string name)
            {

                borrowers.Add(name);

                LibraryItem.NumCopies--;

            }

            public void ReturnItem(string name)
            {

                borrowers.Remove(name);

                LibraryItem.NumCopies++;

            }

            public override void Display()
            {

                LibraryItem.Display();
                foreach (string borrower in borrowers)
                {

                    Console.WriteLine(" borrower: " + borrower);
                }

            }


        }
    }
}


 

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1357674.html