CSC/ECE 517 Summer 2008/wiki1 8 dm: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
'''In Lecture 7, we consider prototype-based programming in Ruby. The example that is given is some what artificial. Can you identify real tasks that are easier with prototype-based programming than with class-based programming? Give the code, at least in outline. '''
'''In Lecture 7, we consider prototype-based programming in Ruby. The example that is given is some what artificial. Can you identify real tasks that are easier with prototype-based programming than with class-based programming? Give the code, at least in outline. '''


After researching this matter, I hit on the idea of using prototype-based programming to handle linked lists. Since the nodes would need to handle different objects, the first node implemented would serve as a prototype for nodes that contain different attributes or more complicated logic. Any container or pointer class that has to deal with a number of functions, you can either use the same 
A prototype-based programming approach in Ruby can be viewed as a creating a basic object with the functionality that is desired.  Once you've created one version of the object, you can then extend that object into much more complex object.  While structural classes can handle the objects, this is simply more efficent to create what you want.
 
In the case of a container class, for example a linked list, the node object has to handle a number of different objects. So declaring a basic node, and then copying it for typed nodes or more complicated logic like back linked list.  


Ex.  
Ex.  
Line 11: Line 13:
  |  @name, @next, @value = name, next, value
  |  @name, @next, @value = name, next, value
  | end
  | end
  |
  | by
  | def getNext
  | def getNext
  |  next
  |  next

Revision as of 23:43, 6 June 2008

Prototype-based Programming

In Lecture 7, we consider prototype-based programming in Ruby. The example that is given is some what artificial. Can you identify real tasks that are easier with prototype-based programming than with class-based programming? Give the code, at least in outline.

A prototype-based programming approach in Ruby can be viewed as a creating a basic object with the functionality that is desired. Once you've created one version of the object, you can then extend that object into much more complex object. While structural classes can handle the objects, this is simply more efficent to create what you want.

In the case of a container class, for example a linked list, the node object has to handle a number of different objects. So declaring a basic node, and then copying it for typed nodes or more complicated logic like back linked list.

Ex.

|class LinkNode
| attr_reader :name, :next, :value
|
| def initialize(name, next,value)
|   @name, @next, @value = name, next, value
| end
| by
| def getNext
|   next
| end
|end
|
|node1 = LinkNode.new('Dave',nul,nul)
|node2 = LinkNode.new('John,nul,nul)
|
|module StringNode
|  def isEmptyString?
|    if value == nul
|     true
|    else
|      false 
|    end
|end
|
|node1.extend(StringNode)


References

  1. Taw's Blog
  2. Softpanorama
  3. Wikipedia Prototype Programming