Author Topic: PowerBASIC CLASS abstraction  (Read 3036 times)

0 Members and 1 Guest are viewing this topic.

Offline James C. Fuller

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 595
  • User-Rate: +11/-8
PowerBASIC CLASS abstraction
« on: September 02, 2010, 04:04:41 PM »
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
'This example is based on the article: How I explained Design Patterns to my wife : Part 1
' Located Here: http://www.codeproject.com/KB/architecture/LearningDesignPatterns1.aspx
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
In my c++ study and research I am starting to grasp the abstraction concept.
One of the key items that helped it to click is the concept that a derived class "is a" base class and can be used
anywhere it's base class can.

I wondered if that was applicatble to PowerBASIC Classes.
Here are my results:

James

Code: [Select]

'SED_PBCC
'=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
#COMPILE EXE
#DIM ALL
'------------------------------------------------------------------------------
'Property Macros
MACRO PropGet(PropName,PropType)=PROPERTY GET PropName() AS PropType:PROPERTY=PropName:END PROPERTY
MACRO PropSet(PropName,PropType)=PROPERTY SET PropName(BYVAL param AS PropType):PropName=param:END PROPERTY
'------------------------------------------------------------------------------
'Return Macros
MACRO Ret_M(RetVal) = WAITKEY$:METHOD=RetVal:EXIT METHOD
MACRO Ret_F(RetVal) = WAITKEY$:FUNCTION=RetVal:EXIT FUNCTION
'------------------------------------------------------------------------------
CLASS cElectricalEquipment
INTERFACE iElectricalEquipment : INHERIT IUNKNOWN
METHOD PowerOn()
END METHOD
METHOD PowerOff()
END METHOD
END INTERFACE
END CLASS
'==============================================================================
CLASS cFan
INTERFACE iFan : INHERIT cElectricalEquipment,iElectricalEquipment
OVERRIDE METHOD PowerOn()
PRINT "Fan is On"
END METHOD
OVERRIDE METHOD PowerOff()
PRINT "Fan is Off"
END METHOD
END INTERFACE
END CLASS
'==============================================================================
CLASS cLight
INTERFACE iLight : INHERIT cElectricalEquipment,iElectricalEquipment
OVERRIDE METHOD PowerOn()
PRINT "Light is On"
END METHOD
OVERRIDE METHOD PowerOff()
PRINT "Light is Off"
END METHOD
END INTERFACE
END CLASS
'==============================================================================
CLASS cSwitch
INSTANCE oEE AS IElectricalEquipment
INTERFACE iSwitch :INHERIT IUNKNOWN
PropGet(oEE,IElectricalEquipment)
PropSet(oEE,IElectricalEquipment)
METHOD On()
oEE.PowerOn()
END METHOD
METHOD Off()
oEE.PowerOff()
END METHOD
END INTERFACE
END CLASS
'==============================================================================
CLASS cFancySwitch
INTERFACE iFancySwitch : INHERIT cSwitch,iSwitch
END INTERFACE
END CLASS
'==============================================================================
CLASS cNormalSwitch
INTERFACE iNormalSwitch : INHERIT cSwitch,iSwitch
END INTERFACE
END CLASS
'==============================================================================
FUNCTION PBMAIN() AS LONG
LOCAL oFan AS iFan
LOCAL oLight As iLight
LOCAL oFs AS iFancySwitch
LOCAL oNs As iNormalSwitch

oFan = CLASS "cFan"
IF ISNOTHING(oFan) THEN
PRINT "NO oFan"
RET_F(0)
END IF

oLight = CLASS "cLight"
IF ISNOTHING(oLight) THEN
PRINT "NO oLight"
RET_F(0)
END IF

oFs = CLASS "cFancySwitch"
IF ISNOTHING(oFs) THEN
PRINT "NO oFs"
RET_F(0)
END IF

oNs = CLASS "cNormalSwitch"
IF ISNOTHING(oNs) THEN
PRINT "NO oNs"
RET_F(0)
END IF


oFs.oEE = oFan
oFs.On()
oFs.Off()

oNs.oEE = oLight
oNs.On()
oNs.Off

oNs.oEE = oFan
oNs.On()
oNs.Off()

WAITKEY$
END FUNCTION