【问题标题】:Decorator Pattern - Access a wrapped ConcreteComponent Value装饰器模式 - 访问包装的 ConcreteComponent 值
【发布时间】:2021-06-05 16:08:13
【问题描述】:

我目前正在研究一个使用装饰器模式的示例。 我当前的代码如下所示:

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("3");
            
            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()
        {
            Console.WriteLine("2");
            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()
        {
            Console.WriteLine("1");
            // base.Display();
            this.libraryItem.Display();
            
            foreach (string borrower in borrowers)
            {
                Console.WriteLine(" borrower: " + borrower);
            }
        }
    }

现在我使用以下代码创建一个可借用装饰视频:

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

我现在要做的是访问 NumCopies - 借用视频对象的属性。预期值为 23,因为我在视频构造函数中指定了该值,但我得到的值为 0。

有人可以帮我如何让程序返回 23 吗?

提前致谢!

【问题讨论】:

    标签: c# design-patterns decorator


    【解决方案1】:

    NumCopies 设为虚拟并覆盖装饰器基类中的行为。

    关于装饰器模式的附注: 在装饰器模式中,abstract class LibraryItem 应该是一个接口。需要装饰器来实现。

    【讨论】:

    • 关于旁注,将基础作为接口并不是强制性的。根据您的要求,它可以是接口和/或抽象类。
    • @YashGupta 从技术上讲你是对的,但界面背后的想法是装饰器必须更改所有虚拟成员。抽象类为错误留有余地,而接口则没有。
    • 这是正确的@verbedr - 只有在所有后代(最终可以移动到某个基本实体)之间存在某种共性(状态/行为)时才应该使用抽象类,否则应该使用接口首选方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多