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