07: OBP Advanced Part I: Classes
"Large Objects built from Smaller Objects"
In this lesson, I will explain the purpose of a Class, Hierarchy & Inheritance, and Templates.
Classes
With Applied Object-Based Programming a Class is a module or section of code that contains an object and all of its associated methods and constructs (arrays, const, globals). Its completely dependent on the use of the Label Convention.
Hierarchies
A Hierarchy can be visualized as an inverted tree of objects, in which the top object gives its properties to the second set of objects. The second set of objects give their properties to the third, so on and so forth. The mechanism of handing down the properties from object above to the object below is called inheritance.
Object Hierarchies simplify the task of building large programs by building on top or combining basic objects to create more complex objects. Identifying basic objects will not be obvious. In some cases you dont have the basic object, therefore you reverse engineer (breakdown) the complex object to identify the basic objects. This will require you to further analyze the purpose and behavior objects as you drill down to the basics. Once the basic objects are identified, you can build on top of these objects or combine them to create larger objects or derivatives.
For example, in our FPS Game we can have a Class called Enemy. The Enemy Class contains a set of properties and methods that are common to all enemies in the game. Such a class is referred to as a Base Class. We can create Derivative Enemy Classes that inherit the properties of the Base Class, in addition its own properties and methods when defined.
In Applied Object-Based Programming, inheritance is accomplished with a Object Pointer as a Property. Each Object Pointer is initilized with its own Constructor providing an additional set of properties that belong to the object in which the pointer resides. See Lesson 10: Code Wizards.
Type enemy Field x# Field y# End Type Type enemytank Field enemy.enemy Field cannon End Type Type enemytroop Field enemy.enemy Field gun End Type
Templates
A Template is a object that shares it properties with multiple objects that contain an Object Key to link to it. By this usage, Objects do not inherit a Templates properties. Templates are ideal for use as object profiles or where multiple objects can use the property values of single object. Modification to the Template's Properties effect all objects that link to it.
;define alienprofile object Type alienprofile Field id% Field eyes% Field mouths Field ears% Field limbs% Field claws Field wings% Field fins% Field hair% Field color% End Function ;draw alienprofile Function alienprofileDraw(this.alienprofile) ;draw# of this\id% ;draw# of this\eyes% ;draw# of this\mouths ;draw# of this\ears% ;draw# of this\limbs% ;draw# of this\claws ;draw# of this\wings% ;draw# of this\fins% ;draw# of this\hair% ;draw# of this\color% End Function ;define alien object Type alien Field ID% Field alienprofileID% Field x# Field y# End Type ;define martian Type martian Field alien.alien Field antennas End Type ;draw aliens based on profile Function alienDraw(this.alien) alienprofileDraw(alienprofile_ID[this\alienprofileID%]) End Function ;create a martian alienprofile template martianprofile.alienprofile=alienprofileNew() ;set martianprofile properties martianprofile\eyes%=3 martianprofile\mouths=2 martianprofile\ears%=4 martianprofile\limbs%=6 martianprofile\claws=0 martianprofile\wings%=0 martianprofile\fins%=2 martianprofile\hair%=2 martianprofile\color%=3 ;create some martians and assign them the template id For martians = 1 to 10 martian.martian=martianNew() martian\alien\alienprofileID%=martianprofile\id% Next ;draw all martians for martian.martian = Each martian alienDraw(martian\alien) next
Exercise:
Extra Credit: In your own words, describe a situation in which using a Template is better suited for the job vs Property Inheritance.