⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 decoratorrealworld.cs

📁 使用C#程序将23个常用设计模式进行列表显示
💻 CS
字号:
using System;
using System.Windows.Forms;
using System.Collections;

//装饰模式(Decorator)
//意图
//    动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
//适用性
//    1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。
//    2.处理那些可以撤消的职责。
//    3.当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。
//        另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

namespace DesignPattern.DecoratorRealWorld
{
    class DecoratorRealWorld : AbstractPattern
    {
        public static void Run(TextBox tbInfo)
        {
            s_tbInfo = tbInfo;
            s_tbInfo.Text = "";

            // Create book 
            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
            DesignPattern.FormMain.OutputInfo("\nMaking video borrowable:"); 
            Borrowable borrowvideo = new Borrowable(video); 
            borrowvideo.BorrowItem("Customer #1"); 
            borrowvideo.BorrowItem("Customer #2"); 
            borrowvideo.Display(); 
            
            // Wait for user 
            //Console.Read();
        }
    }
    // "Component" 
    abstract class LibraryItem 
    {
        private int numCopies;
        
        // Property 
        public int NumCopies 
        {
            get
            {
                return numCopies; 
            }
            set
            {
                numCopies = value; 
            }
        }
        public abstract void Display(); 
    }
    
    // "ConcreteComponent" 
    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() 
        {
            DesignPattern.FormMain.OutputInfo("\nBook ------ "); 
            DesignPattern.FormMain.OutputInfo(" Author: {0}", author); 
            DesignPattern.FormMain.OutputInfo(" Title: {0}", title); 
            DesignPattern.FormMain.OutputInfo(" # Copies: {0}", NumCopies); 
        }
    }
    
    // "ConcreteComponent"
    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() 
        {
            DesignPattern.FormMain.OutputInfo("\nVideo ----- "); 
            DesignPattern.FormMain.OutputInfo(" Director: {0}", director); 
            DesignPattern.FormMain.OutputInfo(" Title: {0}", title); 
            DesignPattern.FormMain.OutputInfo(" # Copies: {0}", NumCopies); 
            DesignPattern.FormMain.OutputInfo(" Playtime: {0}\n", playTime); 
        }
    }
    
    // "Decorator" 
    abstract class Decorator : LibraryItem 
    {
        protected LibraryItem libraryItem; 
        
        // Constructor 
        public Decorator(LibraryItem libraryItem)
        {
            this.libraryItem = libraryItem; 
        }
        
        public override void Display() 
        {
            libraryItem.Display(); 
        }
    }
    
    // "ConcreteDecorator" 
    class Borrowable : Decorator
    {
        protected ArrayList borrowers = new ArrayList();
        
        // 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) 
            {
                DesignPattern.FormMain.OutputInfo(" borrower: " + borrower); 
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -