CSC/ECE 517 Fall 2010/ch1 2c jp: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(22 intermediate revisions by the same user not shown)
Line 1: Line 1:
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][2]  Programmers then define the differences between the default behavior inherited from the prototype to create additional functionality for the extension object.[2]


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.[2]
Many programming languages, such as JavaScript, Self, Io, NewtonScript, Omega, Cecil, Lua, Object-Lisp, Examplars, Agora, ACT-I, Kevo, Moostrap, Obliq, and Garnet, allow for prototype-based inheritance.[3]  The examples in this article will use the Ruby programming language.
== Delegation in Ruby ==
In Ruby, you can define an extension object's prototype by using the SimpleDelegator class.  In order to use the SimpleDelegator class you must include the delegate file.  In this example we  create two instances of class Polygon:
  require 'delegate'
  class Polygon
    attr_reader :sides
    def initialize(sides)
      @sides = sides
    end
  end
  quadrilateral = Polygon.new(4)
  triangle = Polygon.new(3)
Next we create an extension object called rectangle using quadrilateral as our prototype.  As I said before, we use the SimpleDelegator class and pass the prototype as the constructor argument:
  rectangle = SimpleDelegator.new(quadrilateral)
  puts rectangle.sides # => 4
== Extend Method ==
To add functionality in our inheritance hierarchy we use the extend method.  The extend method will add functionality to a particular instance of a class:
  module Area
  def area(length, width)
    length * width
  end
  quadrilateral.extend(Area)
  puts rectangle.area(10, 20) # => 200
According to the definition of delegation, if the method invoked is not part of the calling object then it attempts to find the method in the prototype object.  Since the area method is not a part of the rectangle object the program then looks for the method in the prototype, quadrilateral.
The extend method only adds the functionality to the particular object it is invoked on, not to the class of that object.  The object "quadrilateral" is an instance of class Polygon and so is the object "triangle".  If the object "quadrilateral" is extended with the Area module then it will have access to the area method but the object "triangle" will not.
  triangle.area(10, 20) # => ERROR
== Differences between class-based and prototype-based inheritance ==
Notice in my example that the only class defined was the Polygon class.  If you were to implement this same example in a class-based inheritance you would need classes for Polygon, Quadrilateral, Triangle, and Rectangle.
{| style="background: #F9F9F9;border: 1px solid #AAA; margin: 1em 1em 1em 0px;"
! Class-based Inheritance !! Prototype-based Inheritance
|-
| [[Image:CSC_ECE_517_Fall_2010_ch1_2c_jp1.JPG]] || [[Image:CSC_ECE_517_Fall_2010_ch1_2c_jp2.JPG]]
|}
==References==
[1] [http://en.wikipedia.org/wiki/Prototype-based_programming ''Prototype-based programming'']
[2] Henry Lieberman.  [http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.html ''Using Prototypical Objects to Implement Shared Behavior in Object Oriented Systems'']
[3] A. Lienhard, O. Nierstrasz.  University of Bern.  [http://www.slideshare.net/lienhard/prototypebased-programming-with-javascript#text-version ''Prototype-based Programming with JavaScript'']

Latest revision as of 14:34, 6 October 2010