CSC/ECE 517 Fall 2007/wiki1 6 c9: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by one other user not shown)
Line 12: Line 12:
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.  
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.  


<h3> Mixins </h3>
<h3> Modules and Mixins </h3>




The process of including a module in a class is also called ‘mixing in’ the
A module looks very similar to class. Modules can have methods, constants and classes. Modules provide a means for grouping related methods, constants and classes. They provide a means for implementing multiple inheritance in Ruby. There are two major features that classes possess but that modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.
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.


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 that classes possess but that modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.


<h2>Sample</h2>
<h2>Sample</h2>
Line 109: Line 107:
100000
100000
</pre>
</pre>
The class Employee includes modules Personlike and Emplike. So we can access the methods getname(), getage() and getsalary() as if they are methods in Employee class itself. This provides the functionality of multiple inheritance using mixins.


<h3>Disadvantages of Mixins in Ruby</h3>
<h3>Disadvantages of Mixins in Ruby</h3>
Line 141: Line 141:


</pre>
</pre>




Line 208: Line 206:




 
The class emp implements the interfaces emplike, personlike. So the methods in those interfaces can be implemented by emp class.This provides the functionality of multiple inheritance even thought we are not directly inheriting from a multiple classes.In this example using java,multiple inheritance is achieved by using interfaces.
 
<h2>Implementation in C++ </h2>
<h2>Implementation in C++ </h2>


Line 274: Line 272:
</pre>
</pre>


C++ provides multiple inheritance. So class emp extends personlike and employeelike classes. Now getage(), getsal() and getname() methods can be accessed by emp class object.





Latest revision as of 01:44, 20 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.

Modules and Mixins


A module looks very similar to class. Modules can have methods, constants and classes. Modules provide a means for grouping related methods, constants and classes. They provide a means for implementing multiple inheritance in Ruby. There are two major features that classes possess but that modules do not: instances and inheritance. Classes can have instances (objects), superclasses (parents) and subclasses (children); modules can have none of these.

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.


Sample

 module SampleMixin
     def do_something
        puts “doing something”
     end
 end

 class SampleClass
     include SampleMixin
 end

 s=SampleClass.new
 s.do_something  

Include

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


Consider a scenario of an employee. An employee has the features of a person ( having name,age and other attributes) and also that of an employee( having salary, hours of work and other attributes). In our example, we explain multiple inheritance by showing how modules "Emplike" and "Personlike" are inherited by "Employee" class. Thus we illustrate mixins in ruby.


Implementation 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)


Output:
Federer
26
100000

The class Employee includes modules Personlike and Emplike. So we can access the methods getname(), getage() and getsalary() as if they are methods in Employee class itself. This provides the functionality of multiple inheritance using mixins.

Disadvantages of Mixins in Ruby

A mixin will not warn if the mixin and the user define the same method.(i.e if there are methods with same name in mixin and also in the class, the method in the class is executed without warning the user).

Consider the above example: Incase we include a getsalary method in both the module and also in the class, the method in the class is executed without warning.

module Emplike
     attr_accessor :salary
        def getsalary
         return "#{salary}"
	end
end

class Employee
      include Personlike
      include Emplike    
          def getsalary
            return" This is the salary of user defined getsalary"
          end
end

s=Employee.new
s.salary=100000
puts(s.getsalary)
 

Output:
This is the salary of user defined getsalary


Implementation in Java

The class "emp" implements "emplike" and "personlike" packages in this example.


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);
		
	}
}


The class emp implements the interfaces emplike, personlike. So the methods in those interfaces can be implemented by emp class.This provides the functionality of multiple inheritance even thought we are not directly inheriting from a multiple classes.In this example using java,multiple inheritance is achieved by using interfaces.

Implementation in C++

The class "emp" extends "personlike" and "employeelike" classes.

 #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("Federer",26,100000);
	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++ provides multiple inheritance. So class emp extends personlike and employeelike classes. Now getage(), getsal() and getname() methods can be accessed by emp class object.


Conclusion

C++ multiple inheritance can sometimes become knotted and lead to unordered hierarchy . Java, even though is a single inheritance language can exhibit multiple inheritance using interfaces. However, care has to be taken in Java to implement all the interfaces and it can be a lengthy procedure. 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