Decorator Pattern

 

Comments

 

Extending functionality of an object by deriving from the interface used by the class to be extended constructing by the base constructor and overriding the intended method to be extended, why, so no changes occur on existing code.

 

Code

 

1.	using System;
2.	using System.Collections.Generic;
3.	 
4.	namespace DoFactory.GangOfFour.Decorator.RealWorld
5.	{
6.	  /// 
7.	  /// MainApp startup class for Real-World 
8.	  /// Decorator Design Pattern.
9.	  /// 
10.	  class MainApp
11.	  {
12.	    /// 
13.	    /// Entry point into console application.
14.	    /// 
15.	    static void Main()
16.	    {
17.	      // Create book
18.	      Book book = new Book("Worley", "Inside ASP.NET", 10);
19.	      book.Display();
20.	 
21.	      // Create video
22.	      Video video = new Video("Spielberg", "Jaws", 23, 92);
23.	      video.Display();
24.	 
25.	      // Make video borrowable, then borrow and display
26.	      Console.WriteLine("\nMaking video borrowable:");
27.	 
28.	      Borrowable borrowvideo = new Borrowable(video);
29.	      borrowvideo.BorrowItem("Customer #1");
30.	      borrowvideo.BorrowItem("Customer #2");
31.	 
32.	      borrowvideo.Display();
33.	 
34.	      // Wait for user
35.	      Console.ReadKey();
36.	    }
37.	  }
38.	 
39.	  /// 
40.	  /// The 'Component' abstract class
41.	  /// 
42.	  abstract class LibraryItem
43.	  {
44.	    private int _numCopies;
45.	 
46.	    // Property
47.	    public int NumCopies
48.	    {
49.	      get { return _numCopies; }
50.	      set { _numCopies = value; }
51.	    }
52.	 
53.	    public abstract void Display();
54.	  }
55.	 
56.	  /// 
57.	  /// The 'ConcreteComponent' class
58.	  /// 
59.	  class Book : LibraryItem
60.	  {
61.	    private string _author;
62.	    private string _title;
63.	 
64.	    // Constructor
65.	    public Book(string author, string title, int numCopies)
66.	    {
67.	      this._author = author;
68.	      this._title = title;
69.	      this.NumCopies = numCopies;
70.	    }
71.	 
72.	    public override void Display()
73.	    {
74.	      Console.WriteLine("\nBook ------ ");
75.	      Console.WriteLine(" Author: {0}", _author);
76.	      Console.WriteLine(" Title: {0}", _title);
77.	      Console.WriteLine(" # Copies: {0}", NumCopies);
78.	    }
79.	  }
80.	 
81.	  /// 
82.	  /// The 'ConcreteComponent' class
83.	  /// 
84.	  class Video : LibraryItem
85.	  {
86.	    private string _director;
87.	    private string _title;
88.	    private int _playTime;
89.	 
90.	    // Constructor
91.	    public Video(string director, string title,
92.	      int numCopies, int playTime)
93.	    {
94.	      this._director = director;
95.	      this._title = title;
96.	      this.NumCopies = numCopies;
97.	      this._playTime = playTime;
98.	    }
99.	 
100.	    public override void Display()
101.	    {
102.	      Console.WriteLine("\nVideo ----- ");
103.	      Console.WriteLine(" Director: {0}", _director);
104.	      Console.WriteLine(" Title: {0}", _title);
105.	      Console.WriteLine(" # Copies: {0}", NumCopies);
106.	      Console.WriteLine(" Playtime: {0}\n", _playTime);
107.	    }
108.	  }
109.	 
110.	  /// 
111.	  /// The 'Decorator' abstract class
112.	  /// 
113.	  abstract class Decorator : LibraryItem
114.	  {
115.	    protected LibraryItem libraryItem;
116.	 
117.	    // Constructor
118.	    public Decorator(LibraryItem libraryItem)
119.	    {
120.	      this.libraryItem = libraryItem;
121.	    }
122.	 
123.	    public override void Display()
124.	    {
125.	      libraryItem.Display();
126.	    }
127.	  }
128.	 
129.	  /// 
130.	  /// The 'ConcreteDecorator' class
131.	  /// 
132.	  class Borrowable : Decorator
133.	  {
134.	    protected List borrowers = new List();
135.	 
136.	    // Constructor
137.	    public Borrowable(LibraryItem libraryItem)
138.	      : base(libraryItem)
139.	    {
140.	    }
141.	 
142.	    public void BorrowItem(string name)
143.	    {
144.	      borrowers.Add(name);
145.	      libraryItem.NumCopies--;
146.	    }
147.	 
148.	    public void ReturnItem(string name)
149.	    {
150.	      borrowers.Remove(name);
151.	      libraryItem.NumCopies++;
152.	    }
153.	 
154.	    public override void Display()
155.	    {
156.	      base.Display();
157.	 
158.	      foreach (string borrower in borrowers)
159.	      {
160.	        Console.WriteLine(" borrower: " + borrower);
161.	      }
162.	    }
163.	  }
164.	}
165.

Output

 

Book ------
Author: Worley
Title: Inside ASP.NET
# Copies: 10

Video -----
Director: Spielberg
Title: Jaws
# Copies: 23
Playtime: 92


Making video borrowable:

Video -----
Director: Spielberg
Title: Jaws
# Copies: 21
Playtime: 92

borrower: Customer #1
borrower: Customer #2

  1.               

 

Output

 

Book ------
Author: Worley
Title: Inside ASP.NET
# Copies: 10

Video -----
Director: Spielberg
Title: Jaws
# Copies: 23
Playtime: 92


Making video borrowable:

Video -----
Director: Spielberg
Title: Jaws
# Copies: 21
Playtime: 92

borrower: Customer #1
borrower: Customer #2