Skip to content

Commit e057602

Browse files
committed
add adapter pattern example
1 parent fe76a3e commit e057602

File tree

7 files changed

+209
-36
lines changed

7 files changed

+209
-36
lines changed

Assets/Behavioral Patterns/State Pattern/Exmaple1/StateExample1.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ public Account(string owner)
249249
this._state = new SilverState(0.0, this);
250250
}
251251

252+
//to fix the private field "_owner' is assigned but its value is never used warning
253+
public string GetOwner()
254+
{
255+
return _owner;
256+
}
257+
252258
// Properties
253259
public double Balance
254260
{

Assets/Behavioral Patterns/State Pattern/Exmaple3/NoCash.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,37 @@ public class NoCash : ATMState
1010

1111
ATMMachine atmMachine;
1212

13-
1413
public NoCash(ATMMachine newATMMachine)
15-
{
16-
17-
atmMachine = newATMMachine;
18-
19-
}
20-
21-
public void insertCard()
22-
{
23-
24-
Debug.Log("We don't have any money");
25-
Debug.Log("Your card is ejected");
26-
27-
}
28-
29-
public void ejectCard()
30-
{
31-
32-
Debug.Log("We don't have any money");
33-
Debug.Log("There is no card to eject");
34-
35-
}
36-
37-
public void requestCash(int cashToWithdraw)
38-
{
39-
40-
Debug.Log("We don't have any money");
41-
42-
}
43-
44-
public void insertPin(int pinEntered)
45-
{
46-
47-
Debug.Log("We don't have any money");
48-
49-
}
14+
{
15+
atmMachine = newATMMachine;
16+
}
17+
18+
public void insertCard()
19+
{
20+
Debug.Log("We don't have any money");
21+
Debug.Log("Your card is ejected");
22+
}
23+
24+
public void ejectCard()
25+
{
26+
Debug.Log("We don't have any money");
27+
Debug.Log("There is no card to eject");
28+
29+
}
30+
31+
public void requestCash(int cashToWithdraw)
32+
{
33+
Debug.Log("We don't have any money");
34+
}
35+
36+
public void insertPin(int pinEntered)
37+
{
38+
Debug.Log("We don't have any money");
39+
}
40+
41+
//to fix the private field `NoCash.atmMachine' is assigned but its value is never used warning
42+
ATMMachine GetATMMachine()
43+
{
44+
return atmMachine;
45+
}
5046
}

Assets/Structural Patterns/Adapter 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: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//-------------------------------------------------------------------------------------
2+
// AdapterPatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
9+
//Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
10+
//This real-world code demonstrates the use of a legacy chemical databank. Chemical compound objects access the databank through an Adapter interface.
11+
12+
namespace AdapterPatternExample1
13+
{
14+
public class AdapterPatternExample1 : MonoBehaviour
15+
{
16+
void Start()
17+
{
18+
// Non-adapted chemical compound
19+
Compound unknown = new Compound("Unknown");
20+
unknown.Display();
21+
22+
// Adapted chemical compounds
23+
Compound water = new RichCompound("Water");
24+
water.Display();
25+
26+
Compound benzene = new RichCompound("Benzene");
27+
benzene.Display();
28+
29+
Compound ethanol = new RichCompound("Ethanol");
30+
ethanol.Display();
31+
}
32+
}
33+
34+
/// <summary>
35+
/// The 'Target' class
36+
/// </summary>
37+
class Compound
38+
{
39+
protected string _chemical;
40+
protected float _boilingPoint;
41+
protected float _meltingPoint;
42+
protected double _molecularWeight;
43+
protected string _molecularFormula;
44+
45+
// Constructor
46+
public Compound(string chemical)
47+
{
48+
this._chemical = chemical;
49+
}
50+
51+
public virtual void Display()
52+
{
53+
Debug.Log("\nCompound: " + _chemical + "------");
54+
}
55+
}
56+
57+
/// <summary>
58+
/// The 'Adapter' class
59+
/// </summary>
60+
class RichCompound : Compound
61+
{
62+
private ChemicalDatabank _bank;
63+
64+
// Constructor
65+
public RichCompound(string name)
66+
: base(name)
67+
{
68+
}
69+
70+
public override void Display()
71+
{
72+
// The Adaptee
73+
_bank = new ChemicalDatabank();
74+
75+
_boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
76+
_meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
77+
_molecularWeight = _bank.GetMolecularWeight(_chemical);
78+
_molecularFormula = _bank.GetMolecularStructure(_chemical);
79+
80+
base.Display();
81+
Debug.Log(" Formula: " + _molecularFormula);
82+
Debug.Log(" Weight : " + _molecularWeight);
83+
Debug.Log(" Melting Pt: " + _meltingPoint);
84+
Debug.Log(" Boiling Pt: " + _boilingPoint);
85+
}
86+
}
87+
88+
/// <summary>
89+
/// The 'Adaptee' class
90+
/// </summary>
91+
class ChemicalDatabank
92+
{
93+
// The databank 'legacy API'
94+
public float GetCriticalPoint(string compound, string point)
95+
{
96+
// Melting Point
97+
if (point == "M")
98+
{
99+
switch (compound.ToLower())
100+
{
101+
case "water": return 0.0f;
102+
case "benzene": return 5.5f;
103+
case "ethanol": return -114.1f;
104+
default: return 0f;
105+
}
106+
}
107+
// Boiling Point
108+
else
109+
{
110+
switch (compound.ToLower())
111+
{
112+
case "water": return 100.0f;
113+
case "benzene": return 80.1f;
114+
case "ethanol": return 78.3f;
115+
default: return 0f;
116+
}
117+
}
118+
}
119+
120+
public string GetMolecularStructure(string compound)
121+
{
122+
switch (compound.ToLower())
123+
{
124+
case "water": return "H20";
125+
case "benzene": return "C6H6";
126+
case "ethanol": return "C2H5OH";
127+
default: return "";
128+
}
129+
}
130+
131+
public double GetMolecularWeight(string compound)
132+
{
133+
switch (compound.ToLower())
134+
{
135+
case "water": return 18.015;
136+
case "benzene": return 78.1134;
137+
case "ethanol": return 46.0688;
138+
default: return 0d;
139+
}
140+
}
141+
}
142+
}

Assets/Structural Patterns/Adapter Pattern/Exmaple1/AdapterPatternExample1.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/Adapter Pattern/Exmaple1/AdapterPatternExample1.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)