CSC/ECE 517 Summer 2008/wiki1 8 dm: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== Prototype-based Programming == | == 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. ''' | '''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. ''' | ||
Line 6: | Line 5: | ||
Ex. | Ex. | ||
|class LinkNode | |||
class LinkNode | | attr_reader :name, :next, :value | ||
| | |||
| def initialize(name, next,value) | |||
| @name, @next, @value = name, next, value | |||
| end | |||
| | |||
| def getNext | |||
| next | |||
| end | |||
|end | |||
end | | | ||
|node1 = LinkNode.new('Dave',nul,nul) | |||
node1 = LinkNode.new('Dave',nul,nul) | |node2 = LinkNode.new('John,nul,nul) | ||
node2 = LinkNode.new('John,nul,nul) | | | ||
|module StringNode | |||
module StringNode | | def isEmptyString? | ||
| if value == nul | |||
| true | |||
| else | |||
| false | |||
| end | |||
|end | |||
end | | | ||
|node1.extend(StringNode) | |||
node1.extend(StringNode) | |||
Revision as of 23:03, 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.
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
Ex. |class LinkNode | attr_reader :name, :next, :value | | def initialize(name, next,value) | @name, @next, @value = name, next, value | end | | 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)