Skip to content

Commit 50c5d5f

Browse files
committed
add 3 more patterns example
1 parent d04315c commit 50c5d5f

16 files changed

+436
-0
lines changed

Assets/Structural Patterns/Facade 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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//-------------------------------------------------------------------------------------
2+
// FacadePatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
//This real-world code demonstrates the Facade pattern as a MortgageApplication object which provides a simplified interface to a large subsystem of classes measuring the creditworthyness of an applicant.
9+
10+
namespace FacadePatternExample1
11+
{
12+
public class FacadePatternExample1 : MonoBehaviour
13+
{
14+
void Start()
15+
{
16+
// Facade
17+
Mortgage mortgage = new Mortgage();
18+
19+
// Evaluate mortgage eligibility for customer
20+
Customer customer = new Customer("Ann McKinsey");
21+
bool eligible = mortgage.IsEligible(customer, 125000);
22+
23+
Debug.Log("\n" + customer.Name +
24+
" has been " + (eligible ? "Approved" : "Rejected"));
25+
}
26+
}
27+
28+
/// <summary>
29+
/// The 'Subsystem ClassA' class
30+
/// </summary>
31+
class Bank
32+
{
33+
public bool HasSufficientSavings(Customer c, int amount)
34+
{
35+
Debug.Log("Check bank for " + c.Name);
36+
return true;
37+
}
38+
}
39+
40+
/// <summary>
41+
/// The 'Subsystem ClassB' class
42+
/// </summary>
43+
class Credit
44+
{
45+
public bool HasGoodCredit(Customer c)
46+
{
47+
Debug.Log("Check credit for " + c.Name);
48+
return true;
49+
}
50+
}
51+
52+
/// <summary>
53+
/// The 'Subsystem ClassC' class
54+
/// </summary>
55+
class Loan
56+
{
57+
public bool HasNoBadLoans(Customer c)
58+
{
59+
Debug.Log("Check loans for " + c.Name);
60+
return true;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Customer class
66+
/// </summary>
67+
class Customer
68+
{
69+
private string _name;
70+
71+
// Constructor
72+
public Customer(string name)
73+
{
74+
this._name = name;
75+
}
76+
77+
// Gets the name
78+
public string Name
79+
{
80+
get { return _name; }
81+
}
82+
}
83+
84+
/// <summary>
85+
/// The 'Facade' class
86+
/// </summary>
87+
class Mortgage
88+
{
89+
private Bank _bank = new Bank();
90+
private Loan _loan = new Loan();
91+
private Credit _credit = new Credit();
92+
93+
public bool IsEligible(Customer cust, int amount)
94+
{
95+
Debug.Log(cust.Name + "applies for " + amount+ " loan\n");
96+
97+
bool eligible = true;
98+
99+
// Check creditworthyness of applicant
100+
if (!_bank.HasSufficientSavings(cust, amount))
101+
{
102+
eligible = false;
103+
}
104+
else if (!_loan.HasNoBadLoans(cust))
105+
{
106+
eligible = false;
107+
}
108+
else if (!_credit.HasGoodCredit(cust))
109+
{
110+
eligible = false;
111+
}
112+
113+
return eligible;
114+
}
115+
}
116+
}

Assets/Structural Patterns/Facade Pattern/Exmaple1/FacadePatternExample1.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/Facade Pattern/Exmaple1/FacadePatternExample1.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.

Assets/Structural Patterns/Flyweight 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: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
//-------------------------------------------------------------------------------------
2+
// FlyweightPatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
//This real-world code demonstrates the Flyweight pattern in which a relatively small number of Character objects is shared many times by a document that has potentially many characters.
10+
11+
namespace FlyweightPatternExample1
12+
{
13+
public class FlyweightPatternExample1 : MonoBehaviour
14+
{
15+
void Start()
16+
{
17+
// Build a document with text
18+
string document = "AAZZBBZB";
19+
char[] chars = document.ToCharArray();
20+
21+
CharacterFactory factory = new CharacterFactory();
22+
23+
// extrinsic state
24+
int pointSize = 10;
25+
26+
// For each character use a flyweight object
27+
foreach (char c in chars)
28+
{
29+
pointSize++;
30+
Character character = factory.GetCharacter(c);
31+
character.Display(pointSize);
32+
}
33+
}
34+
}
35+
36+
/// <summary>
37+
/// The 'FlyweightFactory' class
38+
/// </summary>
39+
class CharacterFactory
40+
{
41+
private Dictionary<char, Character> _characters =
42+
new Dictionary<char, Character>();
43+
44+
public Character GetCharacter(char key)
45+
{
46+
// Uses "lazy initialization"
47+
Character character = null;
48+
if (_characters.ContainsKey(key))
49+
{
50+
character = _characters[key];
51+
}
52+
else
53+
{
54+
switch (key)
55+
{
56+
case 'A': character = new CharacterA(); break;
57+
case 'B': character = new CharacterB(); break;
58+
//...
59+
case 'Z': character = new CharacterZ(); break;
60+
}
61+
_characters.Add(key, character);
62+
}
63+
return character;
64+
}
65+
}
66+
67+
/// <summary>
68+
/// The 'Flyweight' abstract class
69+
/// </summary>
70+
abstract class Character
71+
{
72+
protected char symbol;
73+
protected int width;
74+
protected int height;
75+
protected int ascent;
76+
protected int descent;
77+
protected int pointSize;
78+
79+
public abstract void Display(int pointSize);
80+
}
81+
82+
/// <summary>
83+
/// A 'ConcreteFlyweight' class
84+
/// </summary>
85+
class CharacterA : Character
86+
{
87+
// Constructor
88+
public CharacterA()
89+
{
90+
this.symbol = 'A';
91+
this.height = 100;
92+
this.width = 120;
93+
this.ascent = 70;
94+
this.descent = 0;
95+
}
96+
97+
public override void Display(int pointSize)
98+
{
99+
this.pointSize = pointSize;
100+
Debug.Log(this.symbol +
101+
" (pointsize " + this.pointSize + ")");
102+
}
103+
}
104+
105+
/// <summary>
106+
/// A 'ConcreteFlyweight' class
107+
/// </summary>
108+
class CharacterB : Character
109+
{
110+
// Constructor
111+
public CharacterB()
112+
{
113+
this.symbol = 'B';
114+
this.height = 100;
115+
this.width = 140;
116+
this.ascent = 72;
117+
this.descent = 0;
118+
}
119+
120+
public override void Display(int pointSize)
121+
{
122+
this.pointSize = pointSize;
123+
Debug.Log(this.symbol +
124+
" (pointsize " + this.pointSize + ")");
125+
}
126+
127+
}
128+
129+
// ... C, D, E, etc.
130+
131+
/// <summary>
132+
/// A 'ConcreteFlyweight' class
133+
/// </summary>
134+
class CharacterZ : Character
135+
{
136+
// Constructor
137+
public CharacterZ()
138+
{
139+
this.symbol = 'Z';
140+
this.height = 100;
141+
this.width = 100;
142+
this.ascent = 68;
143+
this.descent = 0;
144+
}
145+
146+
public override void Display(int pointSize)
147+
{
148+
this.pointSize = pointSize;
149+
Debug.Log(this.symbol + " (pointsize " + this.pointSize + ")");
150+
}
151+
}
152+
}

Assets/Structural Patterns/Flyweight Pattern/Exmaple1/FlyweightPatternExample1.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/Flyweight Pattern/Exmaple1/FlyweightPatternExample1.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.

0 commit comments

Comments
 (0)