Design Pattern Examples
Overview of object-oriented design patterns
Facade_ComplicatedSubSystem.cs
Go to the documentation of this file.
1
6
7using System.Collections.Generic;
8
10{
15 public enum DeviceTypes
16 {
24 CORE,
28 GTE,
32 PCH,
36 PMC,
37 }
38
39
40 //########################################################################
41 //########################################################################
42
43
48 public class DeviceNode
49 {
53 public bool Visible;
54
58 public string Name;
59
63 public uint Idcode;
64
70
78 public DeviceNode(string name, uint idcode, DeviceTypes tapType, bool initiallyVisible)
79 {
80 Name = name;
81 Idcode = idcode;
82 DeviceType = tapType;
83 Visible = initiallyVisible;
84 }
85 }
86
87
88 //########################################################################
89 //########################################################################
90
91
96 public class DeviceChain
97 {
101 private List<DeviceNode> _nodes = new List<DeviceNode>();
102
106 public string Name;
107
111 public bool IsLocked;
112
117 public DeviceChain(string name)
118 {
119 Name = name;
120 }
121
130 private void _ShowHideNodes(uint nodeSelectMask, bool makeVisible)
131 {
132 uint bitMask = 0x2; // bit 0 is always the DEVICECONTROLLER and is always selected
133 int numNodes = _nodes.Count;
134
135 // Start at the device after the DEVICECONTROLLER
136 for (int index = 1; index < numNodes; ++index)
137 {
138 if ((bitMask & nodeSelectMask) != 0)
139 {
140 _nodes[index].Visible = makeVisible;
141 }
142 bitMask <<= 1;
143 if (bitMask == 0)
144 {
145 // We don't allow more than 32 devices
146 break;
147 }
148 }
149 }
150
151
159 internal void AddNode(DeviceNode node)
160 {
161 if (_nodes.Count > 0 && node.DeviceType == DeviceTypes.DEVICECONTROLLER)
162 {
163 // DEVICECONTROLLER always goes at the start of the list.
164 _nodes.Insert(0, node);
165 }
166 else
167 {
168 _nodes.Add(node);
169 }
170 }
171
172
177 public void ResetVisibility()
178 {
179 foreach (DeviceNode node in _nodes)
180 {
181 if (node.DeviceType != DeviceTypes.DEVICECONTROLLER)
182 {
183 node.Visible = false;
184 }
185 }
186 }
187
194 public void SelectNodes(uint nodeSelectMask)
195 {
196 _ShowHideNodes(nodeSelectMask, true);
197 }
198
205 public void DeselectNodes(uint nodeSelectMask)
206 {
207 _ShowHideNodes(nodeSelectMask, false);
208 }
209
210
219 {
220 List<uint> idcodes = new List<uint>();
221
222 foreach(DeviceNode node in _nodes)
223 {
224 if (node.Visible)
225 {
226 idcodes.Add(node.Idcode);
227 }
228 }
229
230 return idcodes.ToArray();
231 }
232 }
233
234
235 //########################################################################
236 //########################################################################
237
238
254 {
258 private DeviceChain[] _deviceChains = { new DeviceChain("CHAIN0"), new DeviceChain("CHAIN1") };
259
265 {
266 return new Facade_ComplicatedSubSystem();
267 }
268
273 {
274 _deviceChains[0].AddNode(new DeviceNode("DDD_DEVCTRL0", 0x10101010, DeviceTypes.DEVICECONTROLLER, true));
275 _deviceChains[0].AddNode(new DeviceNode("DDD_CORE0", 0x20202020, DeviceTypes.CORE, false));
276 _deviceChains[0].AddNode(new DeviceNode("DDD_GTE0", 0x30303030, DeviceTypes.GTE, false));
277
278 _deviceChains[1].AddNode(new DeviceNode("DDD_DEVCTRL1", 0x10101011, DeviceTypes.DEVICECONTROLLER, true));
279 _deviceChains[1].AddNode(new DeviceNode("DDD_PCH0", 0x40404040, DeviceTypes.PCH, false));
280 _deviceChains[1].AddNode(new DeviceNode("DDD_PMC0", 0x50505050, DeviceTypes.PMC, false));
281 }
282
283
284 //====================================================================
285 // IDeviceNetworkLowLevel methods
286 //====================================================================
287 #region IDeviceNetworkLowLevel methods
293 {
294 return _deviceChains.Length;
295 }
296
305 {
306 bool locked = false;
307
308 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
309 {
310 if (!_deviceChains[chainIndex].IsLocked)
311 {
312 _deviceChains[chainIndex].IsLocked = true;
313 locked = true;
314 }
315 }
316
317 return locked;
318 }
319
328 {
329 bool unlocked = false;
330
331 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
332 {
333 if (_deviceChains[chainIndex].IsLocked)
334 {
335 _deviceChains[chainIndex].IsLocked = false;
336 unlocked = true;
337 }
338 }
339
340 return unlocked;
341 }
342
349 {
350 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
351 {
352 _deviceChains[chainIndex].ResetVisibility();
353 }
354 }
355
365 void IDeviceNetworkLowLevel.EnableDevicesInDeviceChain(int chainIndex, uint deviceselectMask)
366 {
367 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
368 {
369 _deviceChains[chainIndex].SelectNodes(deviceselectMask);
370 }
371 }
372
382 void IDeviceNetworkLowLevel.DisableDevicesInDeviceChain(int chainIndex, uint deviceselectMask)
383 {
384 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
385 {
386 _deviceChains[chainIndex].DeselectNodes(deviceselectMask);
387 }
388 }
389
398 uint[] IDeviceNetworkLowLevel.GetIdcodes(int chainIndex)
399 {
400 uint[] idcodes = new uint[0];
401
402 if (chainIndex >= 0 && chainIndex < _deviceChains.Length)
403 {
404 idcodes = _deviceChains[chainIndex].GetIdCodesForVisibleNodes();
405 }
406 return idcodes;
407 }
408 #endregion
409
410
411 //====================================================================
412 // IDeviceNetworkHighLevel methods
413 //====================================================================
414 #region ITAPNetworkHighLevel methods
415
419 int IDeviceNetworkHighLevel.NumChains
420 {
421 get
422 {
423 return ((IDeviceNetworkLowLevel)this).GetNumChains();
424 }
425 }
426
434 uint[] IDeviceNetworkHighLevel.GetIdcodes(int chainIndex)
435 {
436 uint[] idcodes = new uint[0];
437
438 if (((IDeviceNetworkLowLevel)this).LockDeviceChain(chainIndex))
439 {
440 idcodes = ((IDeviceNetworkLowLevel)this).GetIdcodes(chainIndex);
441 ((IDeviceNetworkLowLevel)this).UnlockDeviceChain(chainIndex);
442 }
443 return idcodes;
444 }
445
454 void IDeviceNetworkHighLevel.EnableDevicesInDeviceChain(int chainIndex, uint selectMask)
455 {
456 if (((IDeviceNetworkLowLevel)this).LockDeviceChain(chainIndex))
457 {
458 ((IDeviceNetworkLowLevel)this).EnableDevicesInDeviceChain(chainIndex, selectMask);
459 ((IDeviceNetworkLowLevel)this).UnlockDeviceChain(chainIndex);
460 }
461 }
462
469 {
470 if (((IDeviceNetworkLowLevel)this).LockDeviceChain(chainIndex))
471 {
472 ((IDeviceNetworkLowLevel)this).ResetDeviceChain(chainIndex);
473 ((IDeviceNetworkLowLevel)this).UnlockDeviceChain(chainIndex);
474 }
475 }
476 #endregion
477 }
478
479
480 //########################################################################
481 //########################################################################
482
483
493 {
498
499
505 {
506 if (_instance == null)
507 {
509 }
510 return _instance;
511 }
512
518 {
519 if (_instance == null)
520 {
522 }
524 }
525 }
526}
Represents a device chain, which is a collection of DeviceNode objects. Part of the Facade pattern ex...
void DeselectNodes(uint nodeSelectMask)
Make invisible one or more devices in the device chain.
bool IsLocked
Whether this device chain is locked for access.
List< DeviceNode > _nodes
The list of TAPNodes on this device chain.
string Name
The Name of this device chain.
void _ShowHideNodes(uint nodeSelectMask, bool makeVisible)
Helper method to show or hide devices on the device chain.
void ResetVisibility()
Resets the device chain so that all devices that are not CLdevices are no longer visible in the devic...
void SelectNodes(uint nodeSelectMask)
Make visible one or more devices in the device chain.
uint[] GetIdCodesForVisibleNodes()
Retrieve a list of idcodes for all devices that are visible in the device chain.
void AddNode(DeviceNode node)
Helper method to add a DeviceNode to the device chain. DeviceNode objects that are of DeviceTypes....
Represents a single device. Part of the Facade pattern example.
DeviceTypes DeviceType
A value from the DeviceTypes enumeration identifying the type of the device.
bool Visible
Whether the device is visible in the device chain.
DeviceNode(string name, uint idcode, DeviceTypes tapType, bool initiallyVisible)
Constructor.
Class factory for the complicated sub-system class. Part of the Facade pattern example.
static IDeviceNetworkLowLevel CreateLowLevelInstance()
Class factory for a singleton instance of the sub-system class.
static ? IDeviceNetworkLowLevel _instance
A singleton instance of the sub-system.
static IDeviceNetworkHighLevel CreateHighLevelInstance()
Class factory for a singleton instance of the sub-system class.
Represents some kind of system that contains multiple device chains. Part of the Facade pattern examp...
static IDeviceNetworkLowLevel CreateInstance()
Class factory for the sub-system class.
bool IDeviceNetworkLowLevel. LockDeviceChain(int chainIndex)
Lock the specified device chain to indicate exclusive access is desired.
Facade_ComplicatedSubSystem()
(private) Constructor. Sets up the device chains.
DeviceChain[] _deviceChains
The list of device chains. In this case, there are two.
Represents a high level view of a complex network of device chains. A device chain can be thought of ...
void DisableDevicesInDeviceChain(int chainIndex)
Resets the given device chain so that all devices except the TAP controller is no longer visible.
void EnableDevicesInDeviceChain(int chainIndex, uint selectMask)
Make visible certain devices in the given device chain. The selectMask value has a bit set for each T...
uint[] GetIdcodes(int chainIndex)
Returns a list of all idcodes from all selected devices in the given device chain.
Represents a network of device chains and the low level access to that network. In general,...
bool LockDeviceChain(int chainIndex)
Lock the specified device chain for exclusive access.
void ResetDeviceChain(int chainIndex)
Reset the visibility of all devices on the specified device chain.
void DisableDevicesInDeviceChain(int chainIndex, uint devicesSelectMask)
Make invisible the specified devices on the specified device chain.
int GetNumChains()
Retrieve the number of device chains available in the network.
void EnableDevicesInDeviceChain(int chainIndex, uint devicesSelectMask)
Make visible the specified devices on the specified device chain.
uint[] GetIdcodes(int chainIndex)
Retrieve a list of idcodes of all visible devices in the given device chain.
bool UnlockDeviceChain(int chainIndex)
Unlock the specified device chain to release exclusive access.
The namespace containing all Design Pattern Examples implemented in C#.
DeviceTypes
Identifies the type of devices that can appear in a device chain. Part of the Facade pattern example.
@ DEVICECONTROLLER
device controller. This is always visible.