CSC/ECE 517 Fall 2010/ch1 2c jp: Difference between revisions
No edit summary |
No edit summary |
||
Line 25: | Line 25: | ||
end | end | ||
captainHook.extend(Evil) | 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!" | peterPan.morality #=> "I'm a good person!" | ||
captainHook.morality #=> "I'm evil!" | 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() | |||
Revision as of 21:43, 5 October 2010
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()
wickedWitch = tinkerBell.clone()
wickedWitch.extend(Evil)
pinocchio.morality robinHood.morality tinkerBell.morality wickedWitch.morality
tinkerBell.fly wickedWitch.fly
In this example, we created three instances of class FairyTaleCharacter. We then extending the object tinkerBell with the ability to fly. At this point, we wish to create a fourth fairy tale character with the ability to flyThen we create a forth instance using the object tinkerBell as the prototype