virtual.bas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  2. 'This example is from the article: How I explained Design Patterns to my wife : Part 1
  3. ' Located Here: http://www.codeproject.com/KB/architecture/LearningDesignPatterns1.aspx
  4. '=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
  5. $CPP
  6. $EXECON
  7. $NOMAIN
  8. Class IElectricalEquipment
  9. public
  10. Virtual sub PowerOn() = 0
  11. Virtual sub PowerOff() = 0
  12. End Class
  13. '==============================================================================
  14. Class Fan Inherits IElectricalEquipment
  15. public
  16. Sub PowerOn()
  17. Sub PowerOff()
  18. End Class
  19. Sub Fan::PowerOn()
  20. Print "Fan is On"
  21. End Sub
  22. Sub Fan::PowerOff()
  23. Print "Fan is Off"
  24. End Sub
  25. '==============================================================================
  26. Class Light Inherits IElectricalEquipment
  27. public
  28. Sub PowerOn()
  29. Sub PowerOff()
  30. End Class
  31. Sub Light::PowerOn()
  32. Print "Light is On"
  33. End Sub
  34. Sub Light::PowerOff()
  35. Print "Light is Off"
  36. End Sub
  37. '==============================================================================
  38. Class Switch
  39. public
  40. Raw equipment As IElectricalEquipment*
  41. Sub On()
  42. Sub Off()
  43. Function get() As IElectricalEquipment Ptr
  44. Sub set(param As IElectricalEquipment*)
  45. End Class
  46. Sub Switch::On()
  47. Print "Switch On the Equipment"
  48. equipment->PowerOn()
  49. End Sub
  50. Sub Switch::Off()
  51. Print "Switch Off the Equipment"
  52. equipment->PowerOff()
  53. End Sub
  54. Function Switch::get() As IElectricalEquipment*
  55. Function = equipment
  56. End Function
  57. Sub Switch::set(p As IElectricalEquipment*)
  58. equipment = p
  59. End Sub
  60. ''==============================================================================
  61. Class NormalSwitch Inherits Switch
  62. End Class
  63. ''==============================================================================
  64. Class FancySwitch Inherits Switch
  65. End Class
  66. Function main( argc As Integer,argv As char Ptr Ptr)
  67. Raw f As IElectricalEquipment Ptr
  68. Raw l AS IElectricalEquipment Ptr
  69. Raw fs As Switch Ptr
  70. Raw ns AS Switch Ptr
  71. f = new Fan()
  72. l = new Light()
  73. fs = new FancySwitch()
  74. ns = new NormalSwitch()
  75. fs->set(f)
  76. fs->On()
  77. fs->Off()
  78. ns->set(l)
  79. ns->On()
  80. ns->Off()
  81. End Function