Guest User

Untitled

a guest
Nov 14th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using System;
  2.  
  3. namespace DoFactory.GangOfFour.Adapter.RealWorld
  4. {
  5. /// <summary>
  6. /// MainApp startup class for Real-World
  7. /// Adapter Design Pattern.
  8. /// </summary>
  9. class MainApp
  10. {
  11. /// <summary>
  12. /// Entry point into console application.
  13. /// </summary>
  14. static void Main()
  15. {
  16. // Non-adapted chemical compound
  17. Compound unknown = new Compound("Unknown");
  18. unknown.Display();
  19.  
  20. // Adapted chemical compounds
  21. Compound water = new RichCompound("Water");
  22. water.Display();
  23.  
  24. Compound benzene = new RichCompound("Benzene");
  25. benzene.Display();
  26.  
  27. Compound ethanol = new RichCompound("Ethanol");
  28. ethanol.Display();
  29.  
  30. // Wait for user
  31. Console.ReadKey();
  32. }
  33. }
  34.  
  35. /// <summary>
  36. /// The 'Target' class
  37. /// </summary>
  38. class Compound
  39. {
  40. protected string _chemical;
  41. protected float _boilingPoint;
  42. protected float _meltingPoint;
  43. protected double _molecularWeight;
  44. protected string _molecularFormula;
  45.  
  46. // Constructor
  47. public Compound(string chemical)
  48. {
  49. this._chemical = chemical;
  50. }
  51.  
  52. public virtual void Display()
  53. {
  54. Console.WriteLine("\nCompound: {0} ------ ", _chemical);
  55. }
  56. }
  57.  
  58. /// <summary>
  59. /// The 'Adapter' class
  60. /// </summary>
  61. class RichCompound : Compound
  62. {
  63. private ChemicalDatabank _bank;
  64.  
  65. // Constructor
  66. public RichCompound(string name)
  67. : base(name)
  68. {
  69. }
  70.  
  71. public override void Display()
  72. {
  73. // The Adaptee
  74. _bank = new ChemicalDatabank();
  75.  
  76. _boilingPoint = _bank.GetCriticalPoint(_chemical, "B");
  77. _meltingPoint = _bank.GetCriticalPoint(_chemical, "M");
  78. _molecularWeight = _bank.GetMolecularWeight(_chemical);
  79. _molecularFormula = _bank.GetMolecularStructure(_chemical);
  80.  
  81. base.Display();
  82. Console.WriteLine(" Formula: {0}", _molecularFormula);
  83. Console.WriteLine(" Weight : {0}", _molecularWeight);
  84. Console.WriteLine(" Melting Pt: {0}", _meltingPoint);
  85. Console.WriteLine(" Boiling Pt: {0}", _boilingPoint);
  86. }
  87. }
  88.  
  89. /// <summary>
  90. /// The 'Adaptee' class
  91. /// </summary>
  92. class ChemicalDatabank
  93. {
  94. // The databank 'legacy API'
  95. public float GetCriticalPoint(string compound, string point)
  96. {
  97. // Melting Point
  98. if (point == "M")
  99. {
  100. switch (compound.ToLower())
  101. {
  102. case "water": return 0.0f;
  103. case "benzene": return 5.5f;
  104. case "ethanol": return -114.1f;
  105. default: return 0f;
  106. }
  107. }
  108. // Boiling Point
  109. else
  110. {
  111. switch (compound.ToLower())
  112. {
  113. case "water": return 100.0f;
  114. case "benzene": return 80.1f;
  115. case "ethanol": return 78.3f;
  116. default: return 0f;
  117. }
  118. }
  119. }
  120.  
  121. public string GetMolecularStructure(string compound)
  122. {
  123. switch (compound.ToLower())
  124. {
  125. case "water": return "H20";
  126. case "benzene": return "C6H6";
  127. case "ethanol": return "C2H5OH";
  128. default: return "";
  129. }
  130. }
  131.  
  132. public double GetMolecularWeight(string compound)
  133. {
  134. switch (compound.ToLower())
  135. {
  136. case "water": return 18.015;
  137. case "benzene": return 78.1134;
  138. case "ethanol": return 46.0688;
  139. default: return 0d;
  140. }
  141. }
  142. }
  143. }
Add Comment
Please, Sign In to add comment