-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathServiceLocatorPattern.cs
194 lines (159 loc) · 4.61 KB
/
ServiceLocatorPattern.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//-------------------------------------------------------------------------------------
// ServiceLocatorPatternExample.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System;
namespace ServiceLocatorPattern
{
public class ServiceLocatorPattern : MonoBehaviour
{
void Start()
{
//注册到服务定位器
TheAudioPlayer audio = new TheAudioPlayer();
ServiceLocator.RegisterService(audio);
}
void Update()
{
//播放声音
if (Input.GetKeyDown(KeyCode.Alpha1))
{
var audio=ServiceLocator.GetAudioService();
if (audio!=null)
{
audio.PlaySound(1);
}
}
//结束声音
if (Input.GetKeyDown(KeyCode.Alpha2))
{
var audio = ServiceLocator.GetAudioService();
if (audio != null)
{
audio.StopSound(1);
}
}
//结束所有声音
if (Input.GetKeyDown(KeyCode.Alpha3))
{
var audio = ServiceLocator.GetAudioService();
if (audio != null)
{
audio.StopAllSounds();
}
}
//注册日志音频类
if (Input.GetKeyDown(KeyCode.Alpha4))
{
ServiceLocator.EnableAudioLogging();
}
}
}
/// <summary>
/// 服务定位器管理类
/// </summary>
public class ServiceLocator
{
static IAudio AudioService_;
static NullAudio NullAudioService_;
public static IAudio GetAudioService() { return AudioService_; }
/// <summary>
/// 注册服务
/// </summary>
/// <param name="service"></param>
public static void RegisterService(IAudio service)
{
if (service == null)
{
// Revert to null service.
AudioService_ = NullAudioService_;
}
else
{
AudioService_ = service;
}
Debug.Log("[ServiceLocator]Finish Register Service!");
}
/// <summary>
/// 注册带日志的音频类
/// </summary>
public static void EnableAudioLogging()
{
// Decorate the existing service.
IAudio service = new LoggedAudio(ServiceLocator.GetAudioService());
// Swap it in.
RegisterService(service);
}
}
/// <summary>
///音频接口
/// </summary>
public interface IAudio
{
void PlaySound(int soundID);
void StopSound(int soundID);
void StopAllSounds();
};
/// <summary>
/// 实际的播放音频的实现类
/// </summary>
public class TheAudioPlayer : IAudio
{
public void PlaySound(int soundID)
{
// Play sound using console audio api...
Debug.Log("Play Sound ! ID = "+soundID.ToString());
}
public void StopSound(int soundID)
{
// Stop sound using console audio api...
Debug.Log("Stop Sound ! ID = " + soundID.ToString());
}
public void StopAllSounds()
{
// Stop all sounds using console audio api...
Debug.Log("Stop All Sound ! ");
}
};
/// <summary>
/// null音频类
/// </summary>
public class NullAudio : IAudio
{
public void PlaySound(int soundID) { /* Do nothing. */ }
public void StopSound(int soundID) { /* Do nothing. */ }
public void StopAllSounds() { /* Do nothing. */ }
};
/// <summary>
/// 带日志的音频类
/// </summary>
class LoggedAudio : IAudio
{
IAudio wrapped_;
public LoggedAudio(IAudio wrapped)
{
wrapped_ = wrapped;
}
public void PlaySound(int soundID)
{
Log("[LoggedAudio]Play sound!");
wrapped_.PlaySound(soundID);
}
public void StopSound(int soundID)
{
Log("[LoggedAudio]Stop sound!");
wrapped_.StopSound(soundID);
}
public void StopAllSounds()
{
Log("[LoggedAudio]Stop all sounds!");
wrapped_.StopAllSounds();
}
private void Log(string message)
{
Debug.LogError(message);
// Code to log message...
}
}
}