Skip to content

Commit 35bfc6a

Browse files
committed
add 3 patterns' example
add 3 patterns' example
1 parent 39b44f6 commit 35bfc6a

41 files changed

Lines changed: 1666 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Binary file not shown.

Assets/Structural Patterns/Adapter Pattern/Exmaple2.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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//-------------------------------------------------------------------------------------
2+
// AdapterPatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
namespace AdapterPatternExample2
9+
{
10+
11+
public class AdapterPatternExample2 : MonoBehaviour
12+
{
13+
void Start()
14+
{
15+
IEnemyAttacker tank = new EnemyTank();
16+
17+
EnemyRobot fredTheRobot = new EnemyRobot();
18+
IEnemyAttacker adapter = new EnemyRobotAdaper(fredTheRobot);
19+
20+
fredTheRobot.ReactToHuman("Hans");
21+
fredTheRobot.WalkForward();
22+
23+
tank.AssignDriver("Frank");
24+
tank.DriveForward();
25+
tank.FireWeapon();
26+
27+
adapter.AssignDriver("Mark");
28+
adapter.DriveForward();
29+
adapter.FireWeapon();
30+
}
31+
}
32+
33+
34+
35+
36+
public interface IEnemyAttacker
37+
{
38+
void FireWeapon();
39+
void DriveForward();
40+
void AssignDriver(string driver);
41+
}
42+
43+
44+
public class EnemyTank : IEnemyAttacker
45+
{
46+
public void FireWeapon()
47+
{
48+
int attackDamage = Random.Range(1, 10);
49+
Debug.Log("Enemy Tank does " + attackDamage + " damage");
50+
}
51+
52+
public void DriveForward()
53+
{
54+
int movement = Random.Range(1, 5);
55+
Debug.Log("Enemy Tank moves " + movement + " spaces");
56+
}
57+
58+
public void AssignDriver(string driver)
59+
{
60+
Debug.Log(driver + " is driving the tank");
61+
}
62+
}
63+
64+
65+
66+
67+
// Adaptee:
68+
public class EnemyRobot
69+
{
70+
public void SmashWithHands()
71+
{
72+
int attackDamage = Random.Range(1, 10);
73+
Debug.Log("Robot causes " + attackDamage + " damage with it hands");
74+
}
75+
76+
public void WalkForward()
77+
{
78+
int movement = Random.Range(1, 3);
79+
Debug.Log("Robot walks " + movement + " spaces");
80+
}
81+
82+
public void ReactToHuman(string driverName)
83+
{
84+
Debug.Log("Robot tramps on " + driverName);
85+
}
86+
}
87+
88+
89+
public class EnemyRobotAdaper : IEnemyAttacker
90+
{
91+
EnemyRobot robot;
92+
93+
public EnemyRobotAdaper(EnemyRobot robot)
94+
{
95+
this.robot = robot;
96+
}
97+
98+
public void FireWeapon()
99+
{
100+
robot.SmashWithHands();
101+
}
102+
103+
public void DriveForward()
104+
{
105+
robot.WalkForward();
106+
}
107+
108+
public void AssignDriver(string driver)
109+
{
110+
robot.ReactToHuman(driver);
111+
}
112+
}
113+
}

Assets/Structural Patterns/Adapter Pattern/Exmaple2/AdapterPatternExample2.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/Exmaple2/AdapterPatternExample2.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/Bridge Pattern/Exmaple2.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: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//-------------------------------------------------------------------------------------
2+
// BridgePatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
/* Decouple an abstraction from its implementation so that the two can vary independently
6+
*
7+
* Progressingly adding functionality while separating out major differences using abstract classes
8+
* (think of remote controls with the same buttons but difference functionality among the buttons for different tvs)
9+
*
10+
* Use when you want to be able rto change both the abstractions (abstract classes) and concrete classes independently
11+
*
12+
* When you want the first abstract class to define rules (Abstract TV)
13+
*
14+
* The concrete class adds additional rules (Concrete TV)
15+
*
16+
* An abstract class has a reference to the device and it defines abstract methods that will be defined (Abstract Remote)
17+
*
18+
* The concrete Remote defines the abstract methods required
19+
*
20+
* */
21+
22+
23+
using UnityEngine;
24+
using System.Collections;
25+
26+
namespace BridgePatternExample2
27+
{
28+
public class BridgePatternExample2 : MonoBehaviour
29+
{
30+
void Start()
31+
{
32+
RemoteButton tv = new TVRemoteMute(new TVDevice(1, 200));
33+
RemoteButton tv2 = new TVRemotePause(new TVDevice(1, 200));
34+
35+
Debug.Log("Test TV with Mute");
36+
tv.ButtonFivePressed();
37+
tv.ButtonSixPressed();
38+
tv.ButtonNinePressed(); // < this one differs on each tv
39+
40+
Debug.Log("Test TV with Pause");
41+
tv2.ButtonFivePressed();
42+
tv2.ButtonSixPressed();
43+
tv2.ButtonNinePressed(); // < this one differs on each tv
44+
}
45+
}
46+
47+
public abstract class EntertainmentDevice
48+
{
49+
public int deviceState;
50+
public int maxSetting;
51+
public int volumeLevel = 0;
52+
53+
public abstract void ButtonFivePressed();
54+
55+
public abstract void ButtonSixPressed();
56+
57+
public void DeviceFeedback()
58+
{
59+
if (deviceState > maxSetting || deviceState < 0)
60+
deviceState = 0;
61+
62+
Debug.Log("On " + deviceState);
63+
}
64+
65+
public void ButtonSevenPressed()
66+
{
67+
volumeLevel++;
68+
Debug.Log("Volume at: " + volumeLevel);
69+
}
70+
71+
public void ButtonEightPressed()
72+
{
73+
volumeLevel--;
74+
Debug.Log("Volume at: " + volumeLevel);
75+
}
76+
}
77+
78+
public class TVDevice : EntertainmentDevice
79+
{
80+
public TVDevice(int newDeviceState, int newMaxSetting)
81+
{
82+
this.deviceState = newDeviceState;
83+
this.maxSetting = newMaxSetting;
84+
}
85+
86+
public override void ButtonFivePressed()
87+
{
88+
Debug.Log("Channel up");
89+
deviceState++;
90+
}
91+
92+
public override void ButtonSixPressed()
93+
{
94+
Debug.Log("Channel down");
95+
deviceState--;
96+
}
97+
}
98+
99+
// abstraction layer
100+
public abstract class RemoteButton
101+
{
102+
private EntertainmentDevice device;
103+
104+
public RemoteButton(EntertainmentDevice device)
105+
{
106+
this.device = device;
107+
}
108+
109+
public void ButtonFivePressed()
110+
{
111+
device.ButtonFivePressed();
112+
}
113+
114+
public void ButtonSixPressed()
115+
{
116+
device.ButtonSixPressed();
117+
}
118+
119+
public void DeviceFeedback()
120+
{
121+
device.DeviceFeedback();
122+
}
123+
124+
public abstract void ButtonNinePressed();
125+
}
126+
127+
128+
// refined abstraction:
129+
public class TVRemoteMute : RemoteButton
130+
{
131+
public TVRemoteMute(EntertainmentDevice device) : base(device)
132+
{
133+
}
134+
135+
public override void ButtonNinePressed()
136+
{
137+
Debug.Log("TV was muted.");
138+
}
139+
}
140+
141+
public class TVRemotePause : RemoteButton
142+
{
143+
public TVRemotePause(EntertainmentDevice device) : base(device)
144+
{
145+
}
146+
147+
public override void ButtonNinePressed()
148+
{
149+
Debug.Log("TV was paused.");
150+
}
151+
}
152+
153+
154+
}

Assets/Structural Patterns/Bridge Pattern/Exmaple2/BridgePatternExample2.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.

0 commit comments

Comments
 (0)