forked from QianMo/Unity-Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterPatternExample2.cs
More file actions
113 lines (86 loc) · 2.53 KB
/
Copy pathAdapterPatternExample2.cs
File metadata and controls
113 lines (86 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//-------------------------------------------------------------------------------------
// AdapterPatternExample2.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
namespace AdapterPatternExample2
{
public class AdapterPatternExample2 : MonoBehaviour
{
void Start()
{
IEnemyAttacker tank = new EnemyTank();
EnemyRobot fredTheRobot = new EnemyRobot();
IEnemyAttacker adapter = new EnemyRobotAdaper(fredTheRobot);
fredTheRobot.ReactToHuman("Hans");
fredTheRobot.WalkForward();
tank.AssignDriver("Frank");
tank.DriveForward();
tank.FireWeapon();
adapter.AssignDriver("Mark");
adapter.DriveForward();
adapter.FireWeapon();
}
}
public interface IEnemyAttacker
{
void FireWeapon();
void DriveForward();
void AssignDriver(string driver);
}
public class EnemyTank : IEnemyAttacker
{
public void FireWeapon()
{
int attackDamage = Random.Range(1, 10);
Debug.Log("Enemy Tank does " + attackDamage + " damage");
}
public void DriveForward()
{
int movement = Random.Range(1, 5);
Debug.Log("Enemy Tank moves " + movement + " spaces");
}
public void AssignDriver(string driver)
{
Debug.Log(driver + " is driving the tank");
}
}
// Adaptee:
public class EnemyRobot
{
public void SmashWithHands()
{
int attackDamage = Random.Range(1, 10);
Debug.Log("Robot causes " + attackDamage + " damage with it hands");
}
public void WalkForward()
{
int movement = Random.Range(1, 3);
Debug.Log("Robot walks " + movement + " spaces");
}
public void ReactToHuman(string driverName)
{
Debug.Log("Robot tramps on " + driverName);
}
}
public class EnemyRobotAdaper : IEnemyAttacker
{
EnemyRobot robot;
public EnemyRobotAdaper(EnemyRobot robot)
{
this.robot = robot;
}
public void FireWeapon()
{
robot.SmashWithHands();
}
public void DriveForward()
{
robot.WalkForward();
}
public void AssignDriver(string driver)
{
robot.ReactToHuman(driver);
}
}
}