04: OBP Basics Part I: Objects
"An Object is a Person, Place, or Thing."
You can look around you now and see an endless number of real-world objects: your dog, your desk, your television set, your bicycle. Each with its own set of properties, behaviors, and purpose. In this lesson , you will learn to program objects by visualizing them in the same manner.
Objects can be modelled from the real-world or completely exist only in software. Both have properties and methods. Let's take a look the following examples:
- A Bicycle is Real-World Object. We could define the properties of a bicycle: handlebar, seat, frame, wheels, pedals, gears, brake. The purpose of a bicycle is to peddle and steer it, traveling from point A to B. From the description of purpose, we can define the methods of our bicycle: pedal, steer, brake.
We can represent our bicycle object in code like so:
Type bicycle Field handlebar Field seat Field frame Field wheels Field pedals Field gears Field brake Field moving End Type Function bicyclePedal(this.bicycle,bicyclepedaldirectionspeed) ;Purpose: Move bicycle forward or backward If this\moving = True Then MoveEntity(this\frame,0,0bicyclepedaldirectionspeed) End Function Function bicycleSteer(this.bicycle,bicylesteerdirection) ;Purpose: Steer bicycle left or right TurnEntity(this\frame,0,bicylesteerdirection,0) End Function Function bicycleBrake(this.bicycle) ;Purpose: Halt bicycle movement this\moving = False End Function
- A LIFO Stack is a Software Object. We could define the properties of a LIFO Stack: array and element pointer. The purpose of a LIFO Stack is push values into it and pop values out of it in a last-in, first-out order. From the description of purpose, we can define the methods of our LIFO Stack: push and pop.
Type stack Field pointer Field array[STACK_MAXELEMENTS%] End Type Function stackPush(this.stack,stackvalue) this\array[this\pointer]=stackvalue this\pointer=this\pointer+1 End Function Function stackPop(this.stack) this\pointer=this\pointer-1 Return this\array[this\pointer] End FunctionAs you can see, if a thing has a purpose, it can be considered an object. In many cases, you will have to define the object's purpose and behavior in order to define its propeties.
Identifying the Objects
The trick to OBP / OOP is identifying the objects. The first step in identifying Objects is to 'discover' the objects within the task, requirement, features list, etc. In designing your application or game, identifying the objects will be seem more like a challenge in the English Language, not a programming one. Consider the following Game description:
"GunVertor is a Multiplayer FPS in which players can assemble a vast assortment of futuristic guns and ammo to eliminate opponents in both indoor and outdoor arenas."
We discover the following objects from the Game's description by parsing out the Nouns:
* Multiplayer (Networking)
* Player
* Gun
* Ammo
* Opponentn
* ArenaNow that we have our objects, we discover the properties of these objects by defining the object's purpose and behavior. What does the object do? How does it do it? As we breakdown the requirements further, we will discover smaller objects.
When identifying objects they should always be labeled with nouns. As we have seen in the FPS Game example, we picked up nouns from the description of the game. Even when you invent object, keep in mind that they should be nouns. Abstract concepts don't qualify as object names. Method names should contain verbs. In any language, actions performed by nouns are specified using verbs (ie: Set, Get, Load, Save)
Prefix adjectives when naming inheriting objects. When a class inherits from a base class, the name for the new class can be determined just by prefixing it with the appropriate adjective. For example, classes inheriting from Gun are called GunRay, GunLaser, etc. Following this convention leads to object names that convey information about the object's inheritance.
Do not add suffixes to object names.
Exercise: