Guest User

Untitled

a guest
Dec 6th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. using Utility;
  2.  
  3. namespace Adapter
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Create new XBox console.
  10. var xbox = new XBox();
  11. // Play with default controller.
  12. xbox.Play();
  13. // Create new PlayStationController.
  14. var playStationController = new PlayStationController();
  15. // Create controller adapter for PlayStation controller.
  16. var adapter = new ControllerAdapter(playStationController);
  17. // Play with adapted controller.
  18. xbox.Play(adapter);
  19.  
  20. Logging.Log("-----------------");
  21.  
  22. // Create new PlayStation console.
  23. var playstation = new PlayStation();
  24. // Play with default controller.
  25. playstation.Play();
  26. // Create new XBoxController.
  27. var xboxController = new XBoxController();
  28. // Create controller adapter for XBox controller.
  29. var adapter2 = new ControllerAdapter(xboxController);
  30. // Play with adapted controller.
  31. playstation.Play(adapter2);
  32. }
  33. }
  34.  
  35. interface IController { }
  36.  
  37. class Controller : IController { }
  38.  
  39. class PlayStationController : Controller { }
  40.  
  41. class XBoxController : Controller { }
  42.  
  43. /// <summary>
  44. /// Used to adapt incompatible controllers within console calls.
  45. /// </summary>
  46. class ControllerAdapter
  47. {
  48. public Controller Controller { get; set; }
  49.  
  50. public ControllerAdapter(Controller controller)
  51. {
  52. // Assign controller to adapter.
  53. Controller = controller;
  54. Logging.Log($"Using adapter on {controller.GetType().Name}.");
  55. }
  56. }
  57.  
  58. interface IConsole
  59. {
  60. void Play();
  61. }
  62.  
  63. class Console : IConsole
  64. {
  65. private Controller _Controller;
  66.  
  67. /// <summary>
  68. /// Controller field with custom set method to output controller being activated.
  69. /// </summary>
  70. protected Controller Controller
  71. {
  72. get { return _Controller; }
  73. set
  74. {
  75. _Controller = value;
  76. Logging.Log($"Plugging {Controller.GetType().Name} into {this.GetType().Name} console.");
  77. }
  78. }
  79.  
  80. public Console() { }
  81.  
  82. /// <summary>
  83. /// Invoke Play call using default controller.
  84. /// </summary>
  85. public void Play()
  86. {
  87. Logging.Log($"Playing with {Controller.GetType().Name} on {this.GetType().Name} console.");
  88. }
  89.  
  90. /// <summary>
  91. /// Invoke Play with associated adapter controller.
  92. /// </summary>
  93. /// <param name="adapter"></param>
  94. public void Play(ControllerAdapter adapter)
  95. {
  96. Controller = adapter.Controller;
  97. Logging.Log($"Playing with {Controller.GetType().Name} on {this.GetType().Name} console.");
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Basic XBox console.
  103. /// </summary>
  104. class XBox : Console
  105. {
  106. public XBox() {
  107. // Associate new XBoxController as default.
  108. Controller = new XBoxController();
  109. }
  110. }
  111.  
  112. /// <summary>
  113. /// Basic PlayStation console.
  114. /// </summary>
  115. class PlayStation : Console
  116. {
  117. public PlayStation()
  118. {
  119. // Associate new PlayStationController as default.
  120. Controller = new PlayStationController();
  121. }
  122. }
  123.  
  124. }
Add Comment
Please, Sign In to add comment