fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. Computer sealedComputer = new Computer();
  8. IComputer computer = new KeyBoard(new Monitor(new Disk(new ComputerAdapter(sealedComputer))));
  9.  
  10. Console.WriteLine(" You are getting a " + computer.getComputer());
  11. }
  12. }
  13.  
  14. public sealed class Computer
  15. {
  16. public string getComputer()
  17. {
  18. return "computer";
  19. }
  20. }
  21.  
  22. public interface IComputer
  23. {
  24. string getComputer();
  25. }
  26.  
  27. public sealed class ComputerAdapter : IComputer
  28. {
  29. private Computer _computer;
  30. public ComputerAdapter(Computer computer)
  31. {
  32. _computer = computer;
  33. }
  34.  
  35. public string getComputer()
  36. {
  37. return _computer.getComputer();
  38. }
  39. }
  40.  
  41. public abstract class ComputerDecorator : IComputer
  42. {
  43. private IComputer _computer;
  44. public ComputerDecorator(IComputer computer)
  45. {
  46. _computer = computer;
  47. }
  48.  
  49. public virtual string getComputer()
  50. {
  51. return _computer.getComputer();
  52. }
  53. }
  54.  
  55. public class Disk : ComputerDecorator
  56. {
  57. public Disk(IComputer c) : base(c)
  58. {
  59. }
  60.  
  61. public override String getComputer()
  62. {
  63. return base.getComputer() + " and a disk";
  64. }
  65. }
  66.  
  67. public class Monitor : ComputerDecorator
  68. {
  69. public Monitor(IComputer c) : base(c)
  70. {
  71. }
  72.  
  73. public override String getComputer()
  74. {
  75. return base.getComputer() + " and a Monitor";
  76. }
  77. }
  78.  
  79. public class KeyBoard : ComputerDecorator
  80. {
  81. public KeyBoard(IComputer c) : base(c)
  82. {
  83. }
  84.  
  85. public override String getComputer()
  86. {
  87. return base.getComputer() + " and a KeyBoard";
  88. }
  89. }
  90.  
Success #stdin #stdout 0.02s 33848KB
stdin
Standard input is empty
stdout
 You are getting a computer and a disk and a Monitor and a KeyBoard