Skip to content

Commit d04315c

Browse files
committed
Add Decorator Pattern Example
1 parent a8c4aba commit d04315c

File tree

7 files changed

+192
-6
lines changed

7 files changed

+192
-6
lines changed
Binary file not shown.

Assets/Structural Patterns/Decorator Pattern/Exmaple1.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//-------------------------------------------------------------------------------------
2+
// DecoratorPatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
//This real-world code demonstrates the Decorator pattern in which 'borrowable' functionality is added to existing library items(books and videos).
10+
11+
namespace DecoratorPatternExample1
12+
{
13+
14+
public class DecoratorPatternExample1 : MonoBehaviour
15+
{
16+
void Start()
17+
{
18+
// Create book
19+
Book book = new Book("Worley", "Inside ASP.NET", 10);
20+
book.Display();
21+
22+
// Create video
23+
Video video = new Video("Spielberg", "Jaws", 23, 92);
24+
video.Display();
25+
26+
// Make video borrowable, then borrow and display
27+
Debug.Log("\nMaking video borrowable:");
28+
29+
Borrowable borrowvideo = new Borrowable(video);
30+
borrowvideo.BorrowItem("Customer #1");
31+
borrowvideo.BorrowItem("Customer #2");
32+
33+
borrowvideo.Display();
34+
}
35+
}
36+
37+
/// <summary>
38+
/// The 'Component' abstract class
39+
/// </summary>
40+
abstract class LibraryItem
41+
{
42+
private int _numCopies;
43+
44+
// Property
45+
public int NumCopies
46+
{
47+
get { return _numCopies; }
48+
set { _numCopies = value; }
49+
}
50+
51+
public abstract void Display();
52+
}
53+
54+
/// <summary>
55+
/// The 'ConcreteComponent' class
56+
/// </summary>
57+
class Book : LibraryItem
58+
{
59+
private string _author;
60+
private string _title;
61+
62+
// Constructor
63+
public Book(string author, string title, int numCopies)
64+
{
65+
this._author = author;
66+
this._title = title;
67+
this.NumCopies = numCopies;
68+
}
69+
70+
public override void Display()
71+
{
72+
Debug.Log("\nBook ------ ");
73+
Debug.Log(" Author: "+ _author);
74+
Debug.Log(" Title: "+ _title);
75+
Debug.Log(" # Copies: "+ NumCopies);
76+
}
77+
}
78+
79+
/// <summary>
80+
/// The 'ConcreteComponent' class
81+
/// </summary>
82+
class Video : LibraryItem
83+
{
84+
private string _director;
85+
private string _title;
86+
private int _playTime;
87+
88+
// Constructor
89+
public Video(string director, string title,
90+
int numCopies, int playTime)
91+
{
92+
this._director = director;
93+
this._title = title;
94+
this.NumCopies = numCopies;
95+
this._playTime = playTime;
96+
}
97+
98+
public override void Display()
99+
{
100+
Debug.Log("\nVideo ----- ");
101+
Debug.Log(" Director: "+ _director);
102+
Debug.Log(" Title: "+ _title);
103+
Debug.Log(" # Copies: "+ NumCopies);
104+
Debug.Log(" Playtime: "+ _playTime+ "\n");
105+
}
106+
}
107+
108+
/// <summary>
109+
/// The 'Decorator' abstract class
110+
/// </summary>
111+
abstract class Decorator : LibraryItem
112+
{
113+
protected LibraryItem libraryItem;
114+
115+
// Constructor
116+
public Decorator(LibraryItem libraryItem)
117+
{
118+
this.libraryItem = libraryItem;
119+
}
120+
121+
public override void Display()
122+
{
123+
libraryItem.Display();
124+
}
125+
}
126+
127+
/// <summary>
128+
/// The 'ConcreteDecorator' class
129+
/// </summary>
130+
class Borrowable : Decorator
131+
{
132+
protected List<string> borrowers = new List<string>();
133+
134+
// Constructor
135+
public Borrowable(LibraryItem libraryItem)
136+
: base(libraryItem)
137+
{
138+
}
139+
140+
public void BorrowItem(string name)
141+
{
142+
borrowers.Add(name);
143+
libraryItem.NumCopies--;
144+
}
145+
146+
public void ReturnItem(string name)
147+
{
148+
borrowers.Remove(name);
149+
libraryItem.NumCopies++;
150+
}
151+
152+
public override void Display()
153+
{
154+
base.Display();
155+
156+
foreach (string borrower in borrowers)
157+
{
158+
Debug.Log(" borrower: " + borrower);
159+
}
160+
}
161+
}
162+
}

Assets/Structural Patterns/Decorator Pattern/Exmaple1/DecoratorPatternExample1.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Structural Patterns/Decorator Pattern/Exmaple1/DecoratorPatternExample1.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Unity-Design-Pattern.CSharp.csproj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@
5656
<Reference Include="UnityEngine.Networking">
5757
<HintPath>Library\UnityAssemblies\UnityEngine.Networking.dll</HintPath>
5858
</Reference>
59-
<Reference Include="UnityEngine.Networking">
60-
<HintPath>Library\UnityAssemblies\UnityEngine.Networking.dll</HintPath>
61-
</Reference>
62-
<Reference Include="UnityEngine.UI">
63-
<HintPath>Library\UnityAssemblies\UnityEngine.UI.dll</HintPath>
64-
</Reference>
6559
<Reference Include="UnityEditor">
6660
<HintPath>Library\UnityAssemblies\UnityEditor.dll</HintPath>
6761
</Reference>
@@ -136,6 +130,7 @@
136130
<Compile Include="Assets\Structural Patterns\Bridge Pattern\Structure\BridgeStructure.cs" />
137131
<Compile Include="Assets\Structural Patterns\Composite Pattern\Exmaple1\CompositePatternExample1.cs" />
138132
<Compile Include="Assets\Structural Patterns\Composite Pattern\Structure\CompositeStructure.cs" />
133+
<Compile Include="Assets\Structural Patterns\Decorator Pattern\Exmaple1\DecoratorPatternExample1.cs" />
139134
<Compile Include="Assets\Structural Patterns\Decorator Pattern\Structure\DecoratorStructure.cs" />
140135
<Compile Include="Assets\Structural Patterns\Facade Pattern\Structure\FacadeStructure.cs" />
141136
<Compile Include="Assets\Structural Patterns\Flyweight Pattern\Structure\FlyweightStructure.cs" />

0 commit comments

Comments
 (0)