CSC/ECE 517 Fall 2010/ch1 2c jp

From Expertiza_Wiki
Revision as of 21:50, 5 October 2010 by Jpvorenk (talk | contribs)
Jump to navigation Jump to search

Prototype-based object oriented programming is the method in which object behavior is defined by existing objects (or prototypes), not classes. In prototype-based programming (also called instance-based programming) prototypes are cloned to create extension objects.[1][4] Programmers then define the differences between the default behavior inherited from the prototype to create additional functionality for the extension object.[4]

All extension objects contain two parts: a list of pointers to their prototype objects, and a set of instructions unique to itself. When a method invocation is sent to an extension object, the object first looks to see if the method is defined in its own set of instructions. If the method is not found within the objects own code, it then looks for the method among the object's prototype methods, and so on. This system of method invocation is called delegation. Programming languages without delegation cannot implement prototype-based programming.[4] The examples in this article will use the Ruby programming language.

First, we create a class called FairyTaleCharacter with one method defined to show the character's morality. We assume all fairy tale characters are good people:

 class FairyTaleCharacter
   def morality
     puts "I'm a good person!"
   end
 end

Next we create two instances of class FairyTaleCharacter:

 pinocchio = FairyTaleCharacter.new
 peterPan = FairyTaleCharacter.new

We want to create a third fairy tale character, Captain Hook, but he is not a good person like we originally assumed. Instead of modifying our original class we can alter only the instance for Captain Hook by using the extend method:

 captainHook = FairyTaleCharacter.new
 module Evil
   def morality
     puts "I'm evil!"
   end
 end
 captainHook.extend(Evil)

Now, if we were to invoke the morality method for these two object we would get different output for each:

 peterPan.morality #=> "I'm a good person!"
 captainHook.morality #=> "I'm evil!"

Finally, we wish to create an object to represent the Wicked Witch of the West. She is evil, and can also fly around on her broomstick. To implement the flying functionality we create the Flying module:

 module Flying
   def fly
     puts "I'm flying!"
   end
 end

Instead of creating a new instance of FairyTaleCharacter for the Wicked Witch and extending it with both the Evil and Flying modules we can use the object representing Captain Hook as a prototype by cloning the object and assigning the clone to our Wicked Witch object:

 wickedWitch = captainHook.clone()

Finally, we augment her instance with the Flying module:

 wickedWitch.extend(Flying)

We can now invoke the morality method and the fly method:

 captainHook.morality #=> "I'm evil!"
 wicketWitch.morality #=> "I'm evil!"
 wickedWitch.fly #=> "I'm flying!"

If we need to create more evil, flying fairy tale characters we can now use wickedWitch as our prototype:

 dragon = wickedWitch.clone()
 donKarnage = wickedWitch.clone() # You know, that guy from TailSpin...