CSC/ECE 517 Fall 2007/wiki1 6 c9: Difference between revisions
No edit summary |
No edit summary |
||
Line 18: | Line 18: | ||
There are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these. | There are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these. | ||
<pre> | |||
Sample | |||
module SampleMixin | |||
module SampleMixin | |||
def do_something | def do_something | ||
puts “doing something” | puts “doing something” | ||
end | end | ||
end | end | ||
class SampleClass | class SampleClass | ||
include SampleMixin | include SampleMixin | ||
end | end | ||
s=SampleClass.new | s=SampleClass.new | ||
s.do_something | s.do_something | ||
</pre> | |||
Line 57: | Line 57: | ||
'''Example in RUBY''' | '''Example in RUBY''' | ||
<pre> | |||
module Personlike | module Personlike | ||
attr_accessor :name | attr_accessor :name | ||
Line 95: | Line 95: | ||
</pre> | |||
Line 101: | Line 101: | ||
'''Implementation in Java''' | '''Implementation in Java''' | ||
<pre> | |||
package assignment; | package assignment; | ||
Line 163: | Line 163: | ||
} | } | ||
</pre> | |||
Line 169: | Line 169: | ||
''' Implementation in C++ ''' | ''' Implementation in C++ ''' | ||
<pre> | |||
#include<iostream.h> | #include<iostream.h> | ||
#include<stdio.h> | #include<stdio.h> | ||
Line 227: | Line 227: | ||
return 0; | return 0; | ||
} | } | ||
</pre> | |||
Line 236: | Line 236: | ||
Ruby is unambiguous, elegant and concise. | Ruby is unambiguous, elegant and concise. | ||
<pre> | |||
''' References Used''' | ''' References Used''' | ||
Line 247: | Line 247: | ||
3)Bowler Ruby | 3)Bowler Ruby | ||
http://www.softwaresummit.com/2006/speakers/BowlerRubyForJavaProgrammers.pdf | http://www.softwaresummit.com/2006/speakers/BowlerRubyForJavaProgrammers.pdf | ||
</pre> |
Revision as of 13:02, 14 September 2007
Topic 6: Mixins
Compare the use of Ruby mixins with how one would solve the same problem in Java or C++. In Java, you might use interfaces or the decorator pattern (p. 91 of Head-First Design Patterns). In C++, you would probably use multiple inheritance. Give code in all three languages and compare the solutions on the basis of verbosity and elegance.
Inheritance and Mixins
Some object-oriented languages (notably C++) support multiple inheritance, where a class can have more than one immediate parent, inheriting functionality from each. Although powerful, this technique can be dangerous, as the inheritance hierarchy can become ambiguous. Ruby offers an interesting and powerful compromise, giving you the simplicity of single inheritance and the power of multiple inheritance. A Ruby class can have only one direct parent (i.e no class can inherit from more than one class) and so Ruby is a single-inheritance language. In cases where you want numerous extra behaviors for a class’s instances, Ruby classes provide the multiple inheritance functionality by using mixins. You can use include any number of mixins (a mixin is like a partial class definition). This provides a controlled multiple-inheritance-like capability with none of the drawbacks.
Mixins
The process of including a module in a class is also called ‘mixing in’ the module – which explains why included modules are often called ‘mixins’. An object can access the instance methods of a module just by including that module using the include method.
There are two major features which classes possess but which modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.
Sample module SampleMixin def do_something puts “doing something” end end class SampleClass include SampleMixin end s=SampleClass.new s.do_something
A couple of points about the include statement. It has nothing to do with files. C programmers use a preprocessor directive called #include to insert the contents of one file into another during compilation. The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require to drag that file in before using include. Second, a Ruby include does not simply copy the module's instance methods into the class. Instead, it makes a reference from the class to the included module. If multiple classes include that module, they'll all point to the same thing. If you change the definition of a method within a module, even while your program is running, all classes that include that module will exhibit the new behavior.
Including Modules From Files
Often it is more useful to define modules in separate files and include them as needed. The first thing you have to do in order to use code from another file is to load that file using the require method, like this: require( "testmod.rb" )
The required file must be in the current directory, on the search path or in a folder listed in the predefined array variable $:. You can add a directory to this array variable using the usual array-append method, << in this way: $: << "C:/mydir" The require method returns a true value if the specified file is successfully loaded; otherwise it returns false. If in doubt, you can simply display the result: puts(require( "testmod.rb" ))
Pre-Defined Modules
The following modules are built in to the Ruby interpreter: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Precision, Process, Signal
Example in RUBY
module Personlike attr_accessor :name attr_accessor :age def getname return "#{name}" end def getage return "#{age}" end end module Emplike attr_accessor :salary def getsalary return "#{salary}" end end class Employee include Personlike include Emplike end s=Employee.new s.name="Federer" s.age=26 s.salary=100000 puts (s.getname) puts(s.getage) puts(s.getsalary)
Implementation in Java
package assignment; public interface personlike { String getname(); int getage(); } package assignment; public interface emplike { float getsalary(); } package assignment; public class emp implements assignment.emplike,assignment.personlike { String name1; int age1; float salary1; emp(String name,int age,float salary) { name1=name;age1=age;salary1=salary; } public String getname() { return name1; } public int getage() { return age1; } public float getsalary() { return salary1; } } package assignment; public class emptest { public static void main(String args[]) { emp ob=new emp("Federer",26,100000); String name=ob.getname(); int age=ob.getage(); float sal=ob.getsalary(); System.out.println("name is "+name+"age is "+age+"salary is "+sal); } }
Implementation in C++
#include<iostream.h> #include<stdio.h> #include<string.h> using namespace std; class personlike { public: char* name; int age; personlike(char* a,int b) { name=new char[10]; strcpy(name,a); age=b; } char* getname() { return name; } int getage() { return age; } }; class employeelike { public: int salary; employeelike(int a) { salary=a; } int getsal() { return salary; } }; class emp:public personlike,public employeelike { public: emp(char* a,int b,int c):personlike(a,b),employeelike(c) { } }; int main() { emp* a=new emp("suhas",22,50000); cout<<a->getname()<<endl;; cout<<a->getage()<<endl; cout<<a->getsal()<<endl; cout<<a->name<<"\t"<<a->age<<"\t"<<a->salary<<endl; return 0; }
C++ multiple inheritance can sometimes become knotted and unordered hierarchy .
Java, even though is single inheritance language can exhibit multiple inheritance using interfaces. But care has to be taken to implement all the interfaces to instantiate objects.
Ruby is unambiguous, elegant and concise.
''' References Used''' 1) The Little Book of Ruby http://www.sapphiresteel.com/The-Little-Book-Of-Ruby 2) Programming Ruby: The Pragmatic Programmer’s guide http://www.rubycentral.com/pickaxe/tut_modules.html 3)Bowler Ruby http://www.softwaresummit.com/2006/speakers/BowlerRubyForJavaProgrammers.pdf