CSC/ECE 517 Fall 2011/ch1 2c rs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(70 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Here we discuss about  interfaces in  Java, modules and  mixins in  Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces.
==Interfaces in Java==
==Interfaces in Java==
Interfaces in Java. An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete ie the methods are only declared but not defined. It can only be extended by other interfaces or implemented by classes. An interface can extend any number of interfaces.
An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by [http://en.wikipedia.org/wiki/Classes_(computer_science) classes]. An interface can extend any number of interfaces.
 
An interface can be defined in the following manner.
  <pre>
   interface <interface name>
   interface <interface name>
     { <constants>
     { <constants>
          <method declarations>
      <method declarations>
      }  
    }
A variable whose declared type is an interface type can hold a reference to an object of a class (or its subclass) that has implemented this interface.
  </pre>
Contractual Obligation
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an abstract class if it fails to implement even one method specified in the interface. An error message is issued by the compiler if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.


'''Example of Interface:'''
 
A variable whose declared type is an interface type can hold a reference to an [http://en.wikipedia.org/wiki/Object_(computer_science) object] of a class (or its subclass) that has implemented this interface.
 
 
'''Contractual Obligation:'''
A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an [http://en.wikipedia.org/wiki/Abstract_class abstract class] if it fails to implement even one method specified in the interface. An error message is issued by the [http://en.wikipedia.org/wiki/Compiler compiler] if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.
 
'''Example of an Interface:'''  
 
In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square' 


              
              
              interface Shape
  <pre>
            {
            interface Shape
                {
                   void draw();   
                   void draw();   
                   void displayArea(float a,float b);
                   void displayArea(float a,float b);
                }
           
            class Square implements Shape
            {
              public void draw()
              {
                System.out.println("Drawing Square");
              }
              public void displayArea(float a,float b)
              {
                  System.out.println("Area is "+a*b);
              }
             }
             }
            class Square implements Shape
 
            {
            public class InfDemo
                  public void draw()
             {
                  {
              public static void main(String args[])
                  System.out.println("Drawing Square");
              {  Square s=new Square();
                  }
                  public void displayArea(float a,float b)
                    {
                          System.out.println("Area is "+a*b);}
                    }
             public class InfDemo{
                  public static void main(String args[])
                  {  Square s=new Square();
                       s.draw();
                       s.draw();
                       s.displayArea(5,5);
                       s.displayArea(5,5);
                  }
              }
           }
           }
</pre>
'''Output:'''
'''Output:'''
   
   
Line 40: Line 57:
Area is 25.0  
Area is 25.0  
</pre>
</pre>
'''Some benefits of Interfaces in Java'''
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.
*They help the developer to design the skeleton behavior for classes.
*An interface can also be used for creating a class that can store application level constants.
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]


==Modules in Ruby==
==Modules in Ruby==
A module in Ruby has the same implementation and is similar to class except a few significant differences:
A  Module<ref>http://www.tutorialspoint.com/ruby/ruby_modules.htm</ref> in ruby is a way of grouping together  methods,variables and classes. It is similar  to the idea of  [http://en.wikipedia.org/wiki/Namespace_(computer_science)  namespace]. A module has the same implementation and is similar to a  [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes ruby class ] except for a few significant differences<ref>http://ruby-doc.org/</ref>:
* Instance of a module cannot be created.
* Instance of a module cannot be created.
* It cannot be inherited by other classes but can be included.
* It cannot be inherited by other classes but can be 'included'. (including  a module in a class definition can roughly mean that the methods are appended to the class).
* The syntax to define module is different.
* The syntax for defining a  module is different.
Example: Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow. The module GoodGuy implements a method bow and tell the caller to bow to the guy because he is a good guy. There is another module BadGuy which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.
 
A module can be defined as follows :
<pre>
module <Module Name>
    #define methods
    #define classes
    #define constants
end
</pre>
 


'''Example of Module:'''
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.
 
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.
<pre>
<pre>
module GoodGuy
module GoodGuy
def GoodGuy.bow
  def GoodGuy.bow
puts "I bow to you. you are a good guy"
    puts "I bow to you. you are a good guy"
  end
end
end
end
</pre>
 
There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.
<pre>
module BadGuy
module BadGuy
def BadGuy.bow
  def BadGuy.bow
puts "I will hit you with a bow.you are a bad guy"
    puts "I will hit you with a bow.you are a bad guy"
end
  end
end
end
</pre>


 
A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names.
require 'GoodGuy'
Note: A module's method can be called by <Module name>.<method name> and module constants are referred as <Module_name>::<method name>
<pre>
require 'GoodGuy' #require is used to load and execute the code once.
require 'BadGuy'
require 'BadGuy'
GoodGuy.bow
GoodGuy.bow
BadGuy.bow
BadGuy.bow
</pre>


==Mixins in Ruby==
===History of Mixins===
Mixins<ref>http://en.wikipedia.org/wiki/Mixin</ref> are not something new. Smalltalk supported them way back in 1971.According to wikipedia,'' Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream  and blended in a combination of extra items and called the item a "Mix-in", his own trademarked term at the time .''


</pre>
==Mixins in Ruby==
===History of Mixins:===
Mixins are not new. Smalltalk supported them back in 1971.Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream (vanilla, chocolate, etc.) and blended in a combination of extra items (nuts, cookies, fudge, etc.) and called the item a "Mix-in", his own trademarked term at the time .
===Mixins===
===Mixins===
Lets assume for a while that the two modules in the above examples are classes. Since Ruby is single-inherited, we cannot implement the same functionality as above through a class. Because to do so, we have to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides the excellent feature of multiple inheritance. We can import the functionality of any module into our class. Thus, the features of this module gets “mixed-in” with our class.<br />
Mixins are used to make certain behavior(s) available to a class.
A class cannot inherit a module but can use the functionality of it by loading it into its definition using the ‘require’ keyword. Then, we can call all the functions in this module as if they belong to our class. If there are two modules having the same method and both modules are required by our class, the name conflict is resolved by using scope resolution operator (::)<br />
The methods of the module do not belong to the class requiring it. In order to make a modules’ method the instance methods of our class, we have to use the include keyword instead of require. However, when we try to include a module which is in a separate file, we have to use both the keywords require and include. Even now, it does not mean that these methods are copied into the class definition. It just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.


Lets assume  that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of  excellent feature of [http://en.wikipedia.org/wiki/Multiple_inheritance multiple inheritance]. Any  functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.<br />


'''Example of Mixins:'''
'''Example of Mixins:'''Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.
<pre>
<pre>
class Actnow
require 'GoodGuy'
require 'GoodGuy'
require 'BadGuy'
require 'BadGuy'
def Act(kind)
class Actnow
if(kind=='good')
    include 'GoodGuy'
GoodGuy.bow
    include 'BadGuy'
end
    def Act(kind)
if (kind =='bad')
      if(kind=='good')
BadGuy.bow
        GoodGuy.bow
end
      end
end
      if (kind =='bad')
        BadGuy.bow
      end
    end
end
end
myobj=Actnow.new
myobj=Actnow.new
myobj.Act('good')
myobj.Act('good')
Line 98: Line 139:


</pre>
</pre>
==PRE-DEFINED MIXIN MODULES ==
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal


Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the <=> operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method between?.  
Note:
The methods of the module do not belong to the class 'requiring' it.  To make a module's method the instance methods of our class, we have to use  ''include''  instead of'' require''. However, to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes <module name>' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.


Enumerable is a mix-in module for enumeration. The including class must provide the method each.  
== Some Pre-defined mixin modules ==
Ruby has the  following built into modules: [http://www.ruby-doc.org/core/classes/Comparable.html Comparable], [http://www.ruby-doc.org/core/classes/Enumerable.html Enumerable],[http://ruby-doc.org/core/classes/FileTest.html FileTest], [http://www.ruby-doc.org/core/classes/GC.html GC], [http://ruby-doc.org/core/classes/Kernel.html Kernel],[http://www.ruby-doc.org/core/classes/Math.html Math], [http://ruby-doc.org/core/classes/ObjectSpace.html ObjectSpace],[http://www.ruby-doc.org/core-1.8.7/classes/Precision.html Precision] , [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal].
 
Following is a brief introduction of the predefined mixin modules.
 
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the <=> operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method 'between?'.
 
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'.
 
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class.
 
 
The''' GC''' module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module.
 
 
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.
'''Math''' is a module containing module functions for basic [http://en.wikipedia.org/wiki/Trigonometric_functions trigonometric] and [http://en.wikipedia.org/wiki/Transcendental_function transcendental] functions.
 
'''ObjectSpace''' is a module which contains routines that interact with the [http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) garbage collection] facility and allows traversing all living objects with an iterator.
 
'''Precision''' is a mixin for concrete numeric classes with precision
 
==Comparable Behavior ==
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them.
 
In Ruby the comparable behavior is achieved by simply defining the <=>operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin.
 
Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.
 
'''Example of Comparable behavior in Ruby:''' <br />
<pre>
class Org
      include Comparable
    attr :name
    attr :revenue
    attr :emplCount
    def initialize(name,revenue,emplCount)
      @name = name
      @revenue=revenue
      @emplCount=emplCount
    end
    def <=>(second) #implementation of <=>
      self.revenue <=> second.revenue # we use the <=> of FixNum
    end
    def to_s
    "#{name}"
    end
end
org1=Org.new('org1',100000,5000);
org2=Org.new('org2',100001,4000);
org3=Org.new('org3',100002,4000);
org4=Org.new('org4',100003,4000);
org5=Org.new('org5',100004,4000);
a=[org1,org2,org3,org4,org5];
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.
</pre>
'''output:'''
<pre>
org1
org2
org3
org4
org5
 
</pre>
 
In Java, there is an interface called [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html Comparable] which declares an abstract method called ''CompareTo''. Several pre-defined classes such as [http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html Float],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Double.html Double],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html Integer],[http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Long.html Long] implement this interface and provide  a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class<ref>http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html</ref>.
 
 
Consider the same scenario as in the above example.The implementation in java is as follows


FileTest is a module containing file test functions; its methods can also be accessed from the File class.
The GC module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module.
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.
Math is a module containing module functions for basic trigonometric and transcendental functions.
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing  all living objects with an iterator.
Precision is a mixin for concrete numeric classes with precision
==Comparable:==
Suppose there is a requirement where we have to compare two objects and determine which is greater. In Java, there is an interface called Comparable which defines in it an abstract method called CompareTo. There is no implementation of this method here. Several pre-defined classes such as Float,Double,Integer,Long implement this interface and give the definition of the CompareTo method. What if we have to compare two objects of our class by comparing a specific attribute of the objects. We need to implement the interface in our class and provide the implementation of the CompareTo method and write our code in there to compare the two objects of that class.
Consider a situation where we have a class Organization and this Organization stores the information about Organizations. Suppose we have to determine between two different organizations as to which is greater by comparing the total revenue of each. The implementation in Java is as follows.


'''Example of Comparable in Java:''' <br />
'''Example of Comparable in Java:''' <br />
Line 123: Line 225:
import java.util.List;
import java.util.List;


//import java.util.*;
public class Org implements Comparable {
public class Org implements Comparable {
String name;
String name;
Line 139: Line 240:
if (this.revenue == ((Org) obj1).revenue)
if (this.revenue == ((Org) obj1).revenue)
          return 0;
          return 0;
      else if ((this.revenue) > ((Org) obj1).revenue)
              else if ((this.revenue) > ((Org) obj1).revenue)
          return 1;
          return 1;
      else
        else
          return -1;
          return -1;
}
}
public String toString() {
public String toString() {
      return "Org " + name  + "\n" ;
      return "Org " + name  + "\n" ;
  }
  }
public static void main(String[] args) {
public static void main(String[] args) {
      List list1 = new ArrayList();
      List list1 = new ArrayList();
Org org1=new Org("org1",1000000,5000);
      Org org1=new Org("org1",1000000,5000);
Org org2=new Org("org2",1000001,4000);
      Org org2=new Org("org2",1000001,4000);
Org org3=new Org("org3",1000002,4000);
              Org org3=new Org("org3",1000002,4000);
Org org4=new Org("org4",1000003,4000);
      Org org4=new Org("org4",1000003,4000);
Org org5=new Org("org5",1000004,4000);
        Org org5=new Org("org5",1000004,4000);
Org org6=new Org("org6",1000005,4000);
      Org org6=new Org("org6",1000005,4000);
list1.add(org1);
      list1.add(org1);
list1.add(org2);
              list1.add(org2);
list1.add(org3);
              list1.add(org3);
list1.add(org4);
      list1.add(org4);
list1.add(org5);
      list1.add(org5);
list1.add(org6);
      list1.add(org6);
      Collections.sort(list1);
      Collections.sort(list1);
      Iterator itr = list1.iterator();
      Iterator itr = list1.iterator();
Line 192: Line 293:
</pre>
</pre>


==Enumerable Behavior ==
'''Enumerable module'''


In this implementation, we had to override the compareTo method and give implementation to determine which organization is greater. Ruby Comparable provides a much simpler approach to this problem. In Ruby, as you will see below, we just need to tell the Comparable module which attribute it has to compare to determine greater object. We need not provide implementation to compare.
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.<br />
'''Example of Comparable in Ruby:''' <br />
If  the  classes which have included the Enumerable module implement the rocket method <=>,  we can also use other methods like min, max ,sort on the collections.<br />
<pre>
class Org
      include Comparable
    attr :name
    attr :revenue
    attr :emplCount
    def initialize(name,revenue,emplCount)
      @name = name
      @revenue=revenue
      @emplCount=emplCount
    end
    def <=>(second)
      self.revenue <=> second.revenue
    end
end
org1=Org.new('org1',100000,5000);
org2=Org.new('org2',100001,4000);
if org1>org2
puts " org1 is greater"
end
if org2>org1
puts " org2 is greater"
end
</pre>
'''output:'''
<pre>
org2 is greater
</pre>
==ENUMERABLE BEHAVIOR ==
ENUMERABLE MODULE
Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and define an iterator ‘each’. This iterator has to return the elements of the collection in turn.<br />
If  the  classes which have included the Enumerable module can implement the rocket method ,  we can also use other methods like min, max ,sort on the collections.<br />
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.<br />
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.<br />
Usage of Enumerables.
'''Usage of Enumerable'''
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.
 
Following is the list of methods provided by the Enumerable mixin.<br />
 
all? ;  any? ;  chunk  collect  collect_concat  count  cycle  detect  drop  drop_while  each_cons  each_entry  each_slice  each_with_index  each_with_object  entries  find  find_all  find_index  first  flat_map  grep  group_by  include?  inject  map  max  max_by  member?  min  min_by  minmax  minmax_by  none?  one?  partition  reduce  reject  reverse_each  select  slice_before  sort  sort_by  take  take_while  to_a  zip 
Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.
<br />


''' To Mix in Enumerable in a class,'''
''' Mix in Enumerable in a class as follows'''


<pre>
<pre>
                   Class MyCollection
                   Class MyCollection
                  include Enumerable
                    include Enumerable
                  #other code
                    #other code
                  def each
                    def each
                  #definiton
                      #definiton
                  end
                    end
                   #othercode
                   #othercode
                   end
                   end
</pre>
</pre>
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.
Some of the built in classes which mixin Enumerable are : [http://ruby-doc.org/core/classes/String.html String], [http://www.ruby-doc.org/core/classes/Hash.html Hash] ,[http://www.ruby-doc.org/core/classes/Array.html Array] ,[http://www.ruby-doc.org/core/classes/Range.html Range] ,[http://www.ruby-doc.org/core/classes/Struct.html Struct].
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.
eg: the Array class ‘s each method yields each element.The Hash class‘s each yields each key-value pair as a two element array.
 
Some Useful  Methods offered by Enumerable:
eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.
map:  modifies each member according to the instructions in a block and returns the modified collection of members.
 
.collect:  similar to map.
 
grep: The grep method ‘searches’ for members using a regular expression  
'''Some Useful  Methods offered by Enumerable:'''
Eg:
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.
*collect:  similar to map.
*grep: The grep method ‘searches’ for members using a regular expression  
eg:
<pre>
<pre>
  (1..10).grep  (5..7)  #=>[5,6,7]
  (1..10).grep  (5..7)  #=>[5,6,7]
</pre>
</pre>
all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.
*all? : It returns true if all elements of a collection satisfy the condition in the block ie the  block never returns false or nil.  If no block is specified it returns true if none of the collection members are false or nil.
'''Example:'''
eg:
<pre>
<pre>
  %w{ant bear cat}.all? {|word| word.length >= 3}  #=> true
  %w{ant bear cat}.all? {|word| word.length >= 3}  #=> true
Line 263: Line 338:
</pre>
</pre>
    
    
any?  
*any?  
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.   
Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.   
<pre>
eg:<pre>
%w{ant bear cat}.any? {|word| word.length >= 3}  #=> true
%w{ant bear cat}.any? {|word| word.length >= 3}  #=> true
%w{ant bear cat}.any? {|word| word.length >= 4}  #=> true
%w{ant bear cat}.any? {|word| word.length >= 4}  #=> true
</pre>
</pre>
    
    
*inject
EXAMPLE: One of the common and useful methods of enumerable  is ‘inject’. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the
One of the common and useful methods of enumerable  is '''‘inject’'''. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the
collection and then applies the operation to the result of this computation and to the third
collection and then applies the operation to the result of this computation and to the third
element, and so on, until all elements in the collection have been used.
element, and so on, until all elements in the collection have been used.


''' Example of Enumerable'''
''' Example of using Enumerable Mixin''': In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits. 
<pre>
<pre>
class EnumerableDemo
class EnumerableDemo
  include Enumerable
include Enumerable
def initialize(string)
    def initialize(string)
  @string=string
      @string=string
end  
    end
def each  
    def each  
       @string.scan(/\d/) do |num|
       @string.scan(/\d/) do |num|
           yield num     
           yield num     
        end
    end
end
end


Line 294: Line 370:
''' output:'''
''' output:'''
<pre>
<pre>
  123007  
  123007 #The numbers in the string "123Bond007" are concatinated and returned.
</pre>
</pre>
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example contains can be mapped to find,to_array can be mapped to collect. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface.  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free.


ENUMERABLE INTERFACE IN JAVA.<br />
 
In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example ''contains'' can be mapped to ''find'',''to_array'' can be mapped to ''collect''. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is  unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free.
 
'''Enumerable Interface in Java'''.<br />
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of  ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects.
Successive calls to the nextElement method return successive elements of the series.  
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:
''' For example, to print all elements of a vector v:'''
<pre>
<pre>
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
Line 308: Line 385:
     }
     }
</pre> <br />
</pre> <br />
hasMoreElements: Tests if this enumeration contains more elements.<br />
'''hasMoreElements''': Tests if this enumeration contains more elements.<br />
nextElement: the next element of this enumeration.<br />
'''nextElement''': the next element of this enumeration.<br />
Note: The functionality of this interface is duplicated by the Iterator interface
 
==Data Mapper:==
==Data Mapper==
In any software development, we often face situations where our code has to interact with a database. In such situations, we have to have knowledge of a query language like SQL to insert into, delete from or modify a database. In modern programming languages, a feature called Object Relational Mapping is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations.  
In any software development, we often face situations where our code has to interact with a [http://en.wikipedia.org/wiki/Database database]. In such situations, we have to have knowledge of a query language like [http://en.wikipedia.org/wiki/Sql SQL] to insert into, delete from or modify a database. In modern programming languages, a feature called [http://en.wikipedia.org/wiki/Object-Relational_Mapping Object Relational Mapping] is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations.  
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide features and is thread-safe(multiple threads can call it without interfering with each other.
 
Below are the steps involved in using the DataMapper
 
In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of  features and is [http://en.wikipedia.org/wiki/Thread_safe thread-safe](multiple [http://en.wikipedia.org/wiki/Thread_(computer_science) threads] can call it without interfering with each other.
Following are the steps involved in using the DataMapper<ref>http://ruby.about.com/od/sinatra/a/datamapper.htm</ref>.
 
 
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.
Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.
''' '''
''' '''
Line 338: Line 419:
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup


  ''' '''
   
<pre>
<pre>
DataMapper.setup(:default, “[dataobject]://[relative-path]”)
DataMapper.setup(:default, “[dataobject]://[relative-path]”)
Line 346: Line 427:


Step-4 – Define models in a class using DataMapper:
Step-4 – Define models in a class using DataMapper:
''' The models can be created as below'''
 
 
The models can be created as below
<pre>
<pre>
# Create and define a class Recipee
# Create and define a class Recipee
Line 359: Line 442:
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=> true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.
This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist.  If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=> true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically.
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table.  
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table.  
'''the code'''
 
<pre>
<pre>
# Create a new record
# Create a new record
Line 369: Line 452:
}
}


Insert: myrecipee.save to save
myrecipee.save #To insert into the DB


Modify: myrecipee.Description=’Tasty and Delicious!’
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB
Delete: Myrecipee.destroy to deletethe tuple in the database
myrecipee.destroy #to delete the tuple in the database


Select: puts myrecipee.inspect
puts myrecipee.inspect  


</pre>
</pre>
DataMappers in JAVA:<br />
In java, Hibernate, iBatis are modern approaches that provide the ORM functionality.  Traditionally, JDBC had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects.<br />
Hibernate defines a language called HQL(Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by Redhat in Java to introduce ORM in Java.




==SINGLETON BEHAVIOR:==
'''DataMapper  in Java'''<br />
According to Wikipedia ,” In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”<br />
In java, [http://en.wikipedia.org/wiki/Hibernate_(Java) Hibernate], [http://en.wikipedia.org/wiki/Ibatis iBatis] are modern approaches that provide the ORM functionality.  Traditionally, [http://en.wikipedia.org/wiki/Jdbc JDBC] had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects<ref>http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/</ref>.<br />
The singleton design pattern is used to have one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.
Hibernate<ref>http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html</ref> defines a language called [http://en.wikipedia.org/wiki/Hibernate_Query_Language HQL](Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by [http://en.wikipedia.org/wiki/Red_hat Redhat] in Java to introduce ORM in Java.
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.
 
==Singleton Behavior==
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern<ref>http://en.wikipedia.org/wiki/Singleton_pattern</ref> is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”''<br />
The singleton design pattern is used when we desire only one instance of some class,  such as one database connection, one logger instance or even one configuration object for an application.
 
 
In Ruby, the singleton behavior<ref>http://www.javabeginner.com/learn-java/java-singleton-design-pattern</ref> can be achieved by simply including the ‘Singleton’ Module.
To use ruby singleton module,  
To use ruby singleton module,  
*’require’ the ‘Singleton’ and then include it in desired class.
*’require’ the ‘Singleton’ and then include it in desired class.
Line 393: Line 480:
* A class method called ‘instance’ is added that instantiates only one instance of the class.
* A class method called ‘instance’ is added that instantiates only one instance of the class.


''' Eg: Usage:'''
''' Eg'''
<pre>
<pre>
   class A
   class A
Line 406: Line 493:
   A.new              #  NoMethodError - new is private ...
   A.new              #  NoMethodError - new is private ...
</pre>
</pre>
Instance is created at the first call of A.instance(), thus this behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.
An instance is created at the first call of A.instance(), thus this behavior is preserved under inheritance and [http://en.wikipedia.org/wiki/Clone_(computing) cloning].This is achieved by marking A.new  as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.
 
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by [http://en.wikipedia.org/wiki/Jvm JVM].
To implement this behavior, the class’s default [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor] is made private. This prevents the direct instantiation of the object.  A  [http://javapapers.com/core-java/explain-the-java-static-modifier/  static]modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.


In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.
To implement this behavior, the class’s default constructor is made private. This prevents the direct instantiation of the object.  A static modifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.
For implementing a singleton pattern, consider the following steps:<br />
For implementing a singleton pattern, consider the following steps:<br />
Step -1:  Provide a default private constructor.<br />
Step -1:  Provide a default private constructor.<br />
Line 416: Line 504:
Step 4: The Object clone method has to be overridden to prevent cloning.<br />
Step 4: The Object clone method has to be overridden to prevent cloning.<br />


''' Example of Singleton:'''
''' Example of Singleton:''' In the following example,We create a class called 'SingletonClass' . In this class,we declare a private static instance variable called 'singletonObject'.The constructor of the class is also made private.A public static accessor method ,'getSingletonObject' is defined which returns the reference to the instance of the class that is stored in the private instance variable singletonObject. Note that the object is created only the first time the accessor is called. Other invocations of the method yield the same object.  Also the 'clone' method of the Object class is overridden to disallow cloning. The class 'SingletonObjectDemo' gets an instance of SingletonClass by using the public accessor method.An attempt to create an object of SingletonClass using 'new' throws compilation error as the constructor is made private.
<pre>
<pre>
class SingletonClass {
class SingletonClass {
Line 448: Line 536:


</pre>
</pre>
==Mixin vs Interfaces==
==Mixin vs Interfaces==
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class.  
*What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class.  
''' Example:'''
''' Example:''' In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method  'name' of the host class . Next we define the host class (Person) that  includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an [http://www.rubyist.net/~slagell/ruby/accessors.html attribute reader] method 'name' and an  [http://ruby.activeventure.com/usersguide/rg/objinitialization.html initialize] method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output.
<pre>
<pre>
module Wisher
module Wisher
def wish
  def wish
  puts "Good Day "+self.name  #name ie attr_reader method of the host class is used even before the class is define.
  puts "Good Day "+self.name  #name ie attr_reader method of the host class is used even before the class is define.
end
  end
end
end


  class Person
  class Person
include Wisher
    include Wisher
def initialize(name,age)
  def initialize(name,age)
@name=name
    @name=name
@age=age
    @age=age
end
  end
attr_reader :name
    attr_reader :name
  end
  end


Line 472: Line 561:
Such a feature is not supported by interfaces in java.
Such a feature is not supported by interfaces in java.


*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at run-time, the classes that include the module use the modified version of the module.
*'''Rewriting''' : When an interface is  rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the  interface he/she is defining  and  specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at [http://en.wikipedia.org/wiki/Run_time_(program_lifecycle_phase) run-time], the classes that include the module use the modified version of the module.
 
*Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an [http://en.wikipedia.org/wiki/Is_a “is-a”] relationship. Mixins are used when there is a “uses a” or [http://en.wikipedia.org/wiki/Has-a “has a”] relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.


*Both Java and Ruby support single inheritance.ie A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. Ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces  that their class Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an “is-a” relationship. Mixins are used when there is a “uses a” or “has a” relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.
*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and  interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the <=> method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like >,<,between? etc only then a runtime error occurs.  


*One analogy that can be used to compare interfaces in java  and mixins in ruby is that interface is like a legal contract while mixin is like a promise. Ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby  there are no such rules to be enforced.
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the <=> method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a exception when the methods like <,>etc  are used because the <=> method has not been implemented.
 
Consider the comparable behavior for example. Both mixin and the interface provide similar behavior. But if a class extends Comparable interface but fails to implement the compareTo method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the  method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like >,<,between? Etc then an runtime error occurs.  
Also
<pre>
<pre>
class A
class A
include Comparable
  include Comparable
  def hi
  def hi
puts "hi"
    puts "hi"
end
  end
  end
end


  a=A.new
  a=A.new


  a.hi  #returns  “hi”.
  a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.
</pre>
</pre>


*Implementing interfaces does not inherit code; it is purely something to help more programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.
* Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.


==Conclusion:==
==Conclusion==
Some of the functionality of mixins is provided by interfaces in popular languages like Java and C#. However, an interface only specifies what the class must support and cannot provide an implementation. Another class, providing an implementation and dependent with the interface, is needed for refactoring common behavior into a single place.Interfaces combined with aspect-oriented programming can produce full fledged mixins in languages that support such features, such as C# or Java. Additionally, through the use of the marker interface pattern, generic programming, and extension methods
We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For [http://en.wikipedia.org/wiki/Refactor refactoring] common behavior into a single place in java (to achieve the power of mixins),we need  another class which  provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programmingcan produce full fledged mixins in Java.


==REFERENCES:==
==References:==
http://ruby.about.com/od/sinatra/a/datamapper.htm . <br />
<references/>
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/<br />
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html<br />
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html<br />
http://www.tutorialspoint.com/ruby/ruby_modules.htm<br />
http://www.javabeginner.com/learn-java/java-singleton-design-pattern<br />
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm<br />
http://ruby-doc.org/<br />
http://download.oracle.com/javase/tutorial<br />
http://en.wikipedia.org/wiki/Mixin<br />
http://en.wikipedia.org/wiki/Singleton_pattern<br />

Latest revision as of 03:57, 1 October 2011

Here we discuss about interfaces in Java, modules and mixins in Ruby.We also briefly discuss about some predefined mixin modules and compare some of the functionalities offered by mixins and the corresponding behaviors if exhibited by the interfaces.

Interfaces in Java

An interface in java is a collection of related methods with empty bodies, constant declarations ,nested types. It is declared using a key word ‘interface’. Constants are implicitly static and final. The methods are implicitly public, an interface cannot be instantiated as it is incomplete i.e, the methods are only declared but not defined. It can only be extended by other interfaces or implemented by classes. An interface can extend any number of interfaces.

An interface can be defined in the following manner.

   interface <interface name>
    { <constants>
      <method declarations>
    }
   


A variable whose declared type is an interface type can hold a reference to an object of a class (or its subclass) that has implemented this interface.


Contractual Obligation: A class that implements an interface has to provide a definition for each method of the inferface or has to be declared as an abstract class if it fails to implement even one method specified in the interface. An error message is issued by the compiler if the class does not define all the methods of an interface it has agreed to define. Even unrelated classes can implement an interface.

Example of an Interface:

In the following example, we create an interface called 'Shape' with the methods 'draw' and 'displayArea'. A class 'Square' implements the interface 'Shape' by provided definitions for the methods 'draw' and 'displayArea'. In the main method we create an object of Square and call the methods. Note that a 'Shape' variable can be used to hold the reference to the object of 'Square'


            interface Shape
                {
                  void draw();  
                  void displayArea(float a,float b);
                }
            
            class Square implements Shape
             { 
               public void draw()
               {
              	  System.out.println("Drawing Square");
               }
               public void displayArea(float a,float b) 
               { 
                  System.out.println("Area is "+a*b);
               }
             }

            public class InfDemo
             {
               public static void main(String args[])
               {  Square s=new Square();
                      s.draw();
                      s.displayArea(5,5);
               }
           }

Output:

Drawing Square
Area is 25.0 

Some benefits of Interfaces in Java

  • Interfaces allow an object to play several roles.ie They can be used to simulate multiple inheritance.
  • They help the developer to design the skeleton behavior for classes.
  • An interface can also be used for creating a class that can store application level constants.
  • Interfaces are very useful when publishing APIs

Modules in Ruby

A Module<ref>http://www.tutorialspoint.com/ruby/ruby_modules.htm</ref> in ruby is a way of grouping together methods,variables and classes. It is similar to the idea of namespace. A module has the same implementation and is similar to a ruby class except for a few significant differences<ref>http://ruby-doc.org/</ref>:

  • Instance of a module cannot be created.
  • It cannot be inherited by other classes but can be 'included'. (including a module in a class definition can roughly mean that the methods are appended to the class).
  • The syntax for defining a module is different.

A module can be defined as follows :

 module <Module Name>
     #define methods
     #define classes
     #define constants
 end


Example: Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.

The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.

module GoodGuy
  def GoodGuy.bow
    puts "I bow to you. you are a good guy"
  end
end

There is another module 'BadGuy' which implements the same method bow but tells the caller to hit the guy with a bow because he is a bad guy.

module BadGuy
  def BadGuy.bow
     puts "I will hit you with a bow.you are a bad guy"
  end
end

A third program which wants to use these modules can load the files using 'require' keyword and can refer to the methods using the qualified names. Note: A module's method can be called by <Module name>.<method name> and module constants are referred as <Module_name>::<method name>

 
require 'GoodGuy' #require is used to load and execute the code once.
require 'BadGuy'
 GoodGuy.bow
 BadGuy.bow

Mixins in Ruby

History of Mixins

Mixins<ref>http://en.wikipedia.org/wiki/Mixin</ref> are not something new. Smalltalk supported them way back in 1971.According to wikipedia, Mixins first appeared in the Symbolics' object-oriented Flavors system (developed by Howard Cannon), which was an approach to object-orientation used in Lisp Machine Lisp. The name was inspired by Steve's Ice Cream Parlor in Somerville, Massachusetts The ice cream shop owner offered a basic flavor of ice cream and blended in a combination of extra items and called the item a "Mix-in", his own trademarked term at the time .

Mixins

Mixins are used to make certain behavior(s) available to a class.

Lets assume that the two modules(module 'GoodGuy'and 'BadGuy') in the above example were classes. Ruby is single-inherited, so the same functionality (ie a person must bow to a good guy and hit the bad guy with a bow). as above cannot be implemented through a class as we need to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides simulation of excellent feature of multiple inheritance. Any functionality of any module can be imported into our class. Thus, the features of the module gets “mixed-in” with our class.

Example of Mixins:Here we define a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.

require 'GoodGuy'
require 'BadGuy'
class Actnow
    include 'GoodGuy'
    include 'BadGuy'
    def Act(kind)
      if(kind=='good')
        GoodGuy.bow
      end
      if (kind =='bad')
        BadGuy.bow
      end
    end
end

myobj=Actnow.new
myobj.Act('good')
myobj.Act('bad')

Note: The methods of the module do not belong to the class 'requiring' it. To make a module's method the instance methods of our class, we have to use include instead of require. However, to include a module which is in a separate file, we have to use both the keywords 'require' and 'include'. This does not mean that these methods are copied into the class definition. Using 'includes <module name>' just references the module’s method from our class. Thus, any modifications made to the method definition in the module even at run time are reflected in the class.

Some Pre-defined mixin modules

Ruby has the following built into modules: Comparable, Enumerable,FileTest, GC, Kernel,Math, ObjectSpace,Precision , Process, Signal.

Following is a brief introduction of the predefined mixin modules.

Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the <=> operator, which compares the receiver against another object, returning -1, 0, or +1 depending on whether the receiver is less than, equal to, or greater than the other object. Comparable uses <=> to implement the conventional comparison operators (<, <=, ==, >=, and >) and the method 'between?'.

Enumerable is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'.

FileTest is a module containing file test functions; its methods can also be accessed from the File class.


The GC module provides an interface to Ruby’s mark and sweep garbage collection mechanism. Some of the underlying methods are also available via the ObjectSpace module.


Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.

Math is a module containing module functions for basic trigonometric and transcendental functions.

ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing all living objects with an iterator.


Precision is a mixin for concrete numeric classes with precision

Comparable Behavior

The comparable functionality is useful when there is a need to compare few objects and maybe even sort them.

In Ruby the comparable behavior is achieved by simply defining the <=>operator(which returns -1,0,1 depending on whether the argument object is greater than, equal to or less-than the calling object) and including the Comparable mixin.

Example of Comparable behavior: Consider a situation where we have a class Organization and this Organization stores the information about several Organizations. Suppose we have to sort different organizations in ascending order as to which is greater by comparing only total revenue of each. The implementation in Ruby is as follows.

Example of Comparable behavior in Ruby:

class Org
      include Comparable
    attr :name
    attr :revenue
    attr :emplCount
    def initialize(name,revenue,emplCount)
      @name = name
      @revenue=revenue
      @emplCount=emplCount
    end
    def <=>(second) #implementation of <=>
      self.revenue <=> second.revenue # we use the <=> of FixNum
    end
    def to_s
    "#{name}"
    end
end
org1=Org.new('org1',100000,5000);
org2=Org.new('org2',100001,4000);
org3=Org.new('org3',100002,4000);
org4=Org.new('org4',100003,4000);
org5=Org.new('org5',100004,4000);
a=[org1,org2,org3,org4,org5];
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.

output:

org1
org2
org3
org4
org5

In Java, there is an interface called Comparable which declares an abstract method called CompareTo. Several pre-defined classes such as Float,Double,Integer,Long implement this interface and provide a definition for the CompareTo method. If we have to compare two objects of a user-defined class by comparing a specific attribute of the objects, we need to implement the interface in our class and provide the implementation of the CompareTo method by writing our code in the method to compare the two objects of that class<ref>http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html</ref>.


Consider the same scenario as in the above example.The implementation in java is as follows


Example of Comparable in Java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Org implements Comparable {
	String name;
	Integer revenue;
	int emplCount;
	public Org(String name,Integer revenue, int emplCount)
	{
		this.name=name;
		this.revenue=revenue;
		this.emplCount=emplCount;
		
	}
	public int compareTo(Object obj1)
	{
		if (this.revenue == ((Org) obj1).revenue)
	           return 0;
       	        else if ((this.revenue) > ((Org) obj1).revenue)
	           return 1;
	        else
	           return -1;
	}
	public String toString() {
	       return "Org " + name  + "\n" ;
	   }
	public static void main(String[] args) {
	      List list1 = new ArrayList();
	      Org org1=new Org("org1",1000000,5000);
	      Org org2=new Org("org2",1000001,4000);
              Org org3=new Org("org3",1000002,4000);
	      Org org4=new Org("org4",1000003,4000);
  	      Org org5=new Org("org5",1000004,4000);
	      Org org6=new Org("org6",1000005,4000);
			
	      list1.add(org1);
              list1.add(org2);
              list1.add(org3);
 	      list1.add(org4);
	      list1.add(org5);
	      list1.add(org6);
	      Collections.sort(list1);
	      Iterator itr = list1.iterator();
	      System.out.println("The list of organizations in the ascending order of revenue are");
	      while(itr.hasNext()){
	          Object element = itr.next();
	          System.out.println(element + "\n");   
	      }
	}
	
}

Output:

The list of organizations in the ascending order of revenue are
Org org1

Org org2

Org org3

Org org4

Org org5

Org org6


Enumerable Behavior

Enumerable module

Various operations are supported by the Ruby Collection classes .Some of such operations are traversing the collection, sorting the collection. We can define classes that can support these features on collections by including the enumerable module and defining an iterator ‘each’. This iterator has to return the elements of the collection in turn.
If the classes which have included the Enumerable module implement the rocket method <=>, we can also use other methods like min, max ,sort on the collections.
Enumerable is a standard mixin implementing operators in terms of the ‘each’ method defined the host class. The host class is that class which includes the enumerable.
Usage of Enumerable


Ruby’s enumerable module has methods for all kinds of operations that can be performed on a collection. Collection objects which can be instances of Array,Hash etc. “mixin” the enumerable module.It gives objects of collections additional collection specific behaviors. These behaviors are given by the 'each' method.

Mix in Enumerable in a class as follows

                   Class MyCollection
                    include Enumerable
                    #other code
                     def each
                      #definiton
                     end
                   #othercode
                   end

Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct. Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.

eg: the Array class ‘s 'each' method yields each element.The Hash class‘s 'each' yields each key-value pair as a two element array.


Some Useful Methods offered by Enumerable:

  • map: modifies each member according to the instructions in a block and returns the modified collection of members.
  • collect: similar to map.
  • grep: The grep method ‘searches’ for members using a regular expression

eg:

 (1..10).grep  (5..7)   #=>[5,6,7]
  • all? : It returns true if all elements of a collection satisfy the condition in the block ie the block never returns false or nil. If no block is specified it returns true if none of the collection members are false or nil.

eg:

 %w{ant bear cat}.all? {|word| word.length >= 3}   #=> true
 %w{ant bear cat}.all? {|word| word.length >= 4}   #=> false
  • any?

Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil.

eg:

%w{ant bear cat}.any? {|word| word.length >= 3}   #=> true
%w{ant bear cat}.any? {|word| word.length >= 4}   #=> true
  • inject

One of the common and useful methods of enumerable is ‘inject’. We can use it in any class that includes enumerable and provides the implemation for ‘each’ method. This method applies a function or operation to the first two elements in the collection and then applies the operation to the result of this computation and to the third element, and so on, until all elements in the collection have been used.

Example of using Enumerable Mixin: In the below example, the class 'EnumerableDemo' includes the Enumerable mixin and provides the definition for 'each' which yields the digits present in a string. When inject method with the argument +,is called on the object of the class it returns a string of digits.

class EnumerableDemo
 include Enumerable
    def initialize(string)
       @string=string
    end
 
    def each 
       @string.scan(/\d/) do |num|
           yield num    
    end
end


ed=EnumerableDemo.new("123Bond007")
puts ed.inject(:+)

output:

 123007 #The numbers in the string "123Bond007" are concatinated and returned.


In Java, the Collection interface specifies some methods similar to the methods of Enumerable module. For example contains can be mapped to find,to_array can be mapped to collect. etc. But the methods are also not as powerful as those of the Enumerable module methods. Also the implementation of these methods is not available unless we inherit from a class which implements this interface. This is unlike mixins in ruby where implementation of ‘each’ method provides us several useful collection methods for free.

Enumerable Interface in Java.
Java provides the Enumeration Interface. But its functionality is different from the Enumerable module of ruby.In Java, the Enumeration interface defines the methods by which you can enumerate (obtain one at a time) the elements in a collection of objects. Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:

     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {
         System.out.println(e.nextElement());

     }


hasMoreElements: Tests if this enumeration contains more elements.
nextElement: the next element of this enumeration.

Data Mapper

In any software development, we often face situations where our code has to interact with a database. In such situations, we have to have knowledge of a query language like SQL to insert into, delete from or modify a database. In modern programming languages, a feature called Object Relational Mapping is provided. This features allow the user to access the database tuples as if they were objects. All the database operations such as insert, delete,update,etc. are implemented in the language using objects. The programmer need not have a firsthand knowledge of a query language to perform these operations.


In Ruby, Object Relational Mapping is implemented using DataMapper. The DataMapper provides wide variety of features and is thread-safe(multiple threads can call it without interfering with each other. Following are the steps involved in using the DataMapper<ref>http://ruby.about.com/od/sinatra/a/datamapper.htm</ref>.


Step 1: Install the dm-core ruby gem – the core of the DataMapper library. This gem has no external dependencies and so, we can install it in the same way we install other gems in ruby.

$sudo gem install dm-core

Step 2: We now need to install an adapter which allows the DataMapper to talk to the database. This is specific to each database provider. '

Eg: For SQLite 3

$ sudo apt-get install libsqlite3-dev
$ sudo gem install dm-sqlite-adapter


Step 3: We then need to require the dm-core gem in our application.

Require ‘rubygems’
Require’data_mapper’
Require ‘dm-core’

Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup


DataMapper.setup(:default, “[dataobject]://[relative-path]”)
For SQLite, this would be something like this.
DataMapper.setup( :default, "sqlite3://#databases/myown.db" )

Step-4 – Define models in a class using DataMapper:


The models can be created as below
# Create and define a class Recipee
  class Recipee
  include DataMapper::Resource

  property :Name, String: key => true
  property :type, String
  property :Description, text
 end

This model can be called using the DataMapper.auto_migrate command. This creates the table if it does not already exist. If the table already exists, it modifies the columns in this table. We can specify primary key by using the key=> true. The serial type is an auto increment integer key. We can create a column ID and define its type as serial to make it a key automatically. Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table.

# Create a new record
myrecipee = Recipee.new
myrecipee.attributes = {
  :name => 'OrangeJuice',
  :type => 'beverage',
  :Description => 'Tasty!'
}

myrecipee.save #To insert into the DB

myrecipee.Description=’Tasty and Delicious!’ #to modify the DB
 
myrecipee.destroy #to delete the tuple in the database

puts myrecipee.inspect 


DataMapper in Java
In java, Hibernate, iBatis are modern approaches that provide the ORM functionality. Traditionally, JDBC had to be used to access the databases.The functionalities provided by Hibernate are similar to that of DataMapper in ruby- it can access the database as objects<ref>http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/</ref>.
Hibernate<ref>http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html</ref> defines a language called HQL(Hybernate Query Language). HQL can be said to be the object oriented version of SQL. Java was created in 1995, but hibernate did not come into existence until 2001. Hibernate was developed by Redhat in Java to introduce ORM in Java.

Singleton Behavior

According to Wikipedia ,” In software engineering, the singleton pattern<ref>http://en.wikipedia.org/wiki/Singleton_pattern</ref> is a design pattern that is used to restrict instantiation of a class to one object. (This concept is also sometimes generalized to restrict the instance to a specific number of objects . This is useful when exactly one object is needed to coordinate actions across the system.”
The singleton design pattern is used when we desire only one instance of some class, such as one database connection, one logger instance or even one configuration object for an application.


In Ruby, the singleton behavior<ref>http://www.javabeginner.com/learn-java/java-singleton-design-pattern</ref> can be achieved by simply including the ‘Singleton’ Module. To use ruby singleton module,

  • ’require’ the ‘Singleton’ and then include it in desired class.
  • Use the instance method to get the instance you need.

Ruby does the following when a singleton module is included in a class.

  • New method is made private so that it can’t be used for instantiation.
  • A class method called ‘instance’ is added that instantiates only one instance of the class.

Eg

   class A
      include Singleton
      # ...
   end

this ensures that only one instance of A be created.

  a,b  = A.instance, A.instance
  a == b    # => true
  A.new               #  NoMethodError - new is private ...

An instance is created at the first call of A.instance(), thus this behavior is preserved under inheritance and cloning.This is achieved by marking A.new as private and providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.

In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM. To implement this behavior, the class’s default constructor is made private. This prevents the direct instantiation of the object. A staticmodifier is applied to the instance method that returns the object as it then makes this method a class level method that can be accessed without creating an object.

For implementing a singleton pattern, consider the following steps:
Step -1: Provide a default private constructor.
Step-2: Define a method to obtain the reference to the singleton Object.
Step 3: The Access method has to be made synchronized to prevent Thread Problems.
Step 4: The Object clone method has to be overridden to prevent cloning.

Example of Singleton: In the following example,We create a class called 'SingletonClass' . In this class,we declare a private static instance variable called 'singletonObject'.The constructor of the class is also made private.A public static accessor method ,'getSingletonObject' is defined which returns the reference to the instance of the class that is stored in the private instance variable singletonObject. Note that the object is created only the first time the accessor is called. Other invocations of the method yield the same object. Also the 'clone' method of the Object class is overridden to disallow cloning. The class 'SingletonObjectDemo' gets an instance of SingletonClass by using the public accessor method.An attempt to create an object of SingletonClass using 'new' throws compilation error as the constructor is made private.

class SingletonClass {

	private static SingletonClass singletonObject;
	/** A private Constructor prevents any other class from instantiating. */
	private SingletonClass() {
		//	 Optional Code
	}
	public static synchronized SingletonClass getSingletonObject() {
		if (singletonObject == null) {
			singletonObject = new SingletonClass();
		}
		return singletonObject;
	}
	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}
}

public class SingletonObjectDemo {

	public static void main(String args[]) {
		//		SingletonClass obj = new SingletonClass();
                //Compilation error not allowed
		SingletonClass obj = SingletonClass.getSingletonObject();
		// Your Business Logic
		System.out.println("Singleton object obtained");
	}
}

Mixin vs Interfaces

  • What makes mixins so powerful is that the methods of the host class(the class that includes the module) can be accessed in the module even before we define the host class.

Example: In the following example,we define a module 'Wisher' which defines a method 'wish' that uses a method 'name' of the host class . Next we define the host class (Person) that includes 'Wisher' and hence obtains the 'wish' method of the module.The 'Person' class contains an attribute reader method 'name' and an initialize method. We then create an object 'p' of class 'Person' and call the method 'wish' on it.The 'wish' method uses method 'name' of Person to return a string as the output.

module Wisher
  def wish
   puts "Good Day "+self.name  #name ie attr_reader method of the host class is used even before the class is define.
  end
end

 class Person
    include Wisher
  def initialize(name,age)
    @name=name
    @age=age
  end
    attr_reader :name
 end

p=Person.new (“James”,4)
p.wish   #returns Good Day James.

Such a feature is not supported by interfaces in java.

  • Rewriting : When an interface is rewritten, all classes that implemented the older version are now broken because they no longer implement the interface. Hence the programmer has to try to anticipate all uses for the interface he/she is defining and specify it completely from the beginning. Such a problem is not caused in Ruby. Even if the module definition is changed at run-time, the classes that include the module use the modified version of the module.
  • Both Java and Ruby support single inheritance.i.e, A class can inherit features from a single class. Multiple inheritance is achieved in Java through interfaces and in Ruby through Mixins. ie A java class can extend only one class but can implement several interfaces. Hence objects can have multiple types ie the type of their own class and the type of the interfaces that their class implements. Similarly a Ruby class can be subclassed from a single class but can include many modules.(mixins). In Ruby, inheritance and mixins allow us to write code at one place and reuse it in other classes. Inheritance is used when there is an “is-a” relationship. Mixins are used when there is a “uses a” or “has a” relationship. However there is no such advantage with interfaces in java . Hence there can be misuse of inheritance as even unrelated classes can implement an interface.
  • One analogy that can be used to compare interfaces in java and mixins in ruby is that interface is like a legal contract while mixin is like a promise. ie a java interface is all about meeting the rules of the extra methods that must be provided where as in ruby there are no such rules to be enforced. Consider the comparable behavior for example. Both mixins and interfaces provide similar behavior. But if a class extends' Comparable' interface and fails to implement the 'compareTo' method a compile time error occurs even if the object of the class does not attempt to use the comparable behavior. But in cases of mixins, if a class includes ‘Comparable’ mixin and does not implement the <=> method there are no errors if the object of the class does not attempt to use the comparable behavior. Hence we can think of it as a promise. If the object uses the comparable behavior ie calls methods like >,<,between? etc only then a runtime error occurs.


eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the <=> method. When an object 'a' of class 'A' is created and method 'hi' is invoked on it, there is no exception. But there will be a exception when the methods like <,>etc are used because the <=> method has not been implemented.

class A
  include Comparable
 def hi
    puts "hi"
 end
end

 a=A.new

 a.hi  #returns  “hi”.  In java,there would be a compilation error in such a scenario since the methods of the Interface are not implemented.
  • Also classes implementing interfaces do not inherit code. ie there is no code reusability.An interface is purely something to help programs type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.

Conclusion

We have seen that some of the functionality of mixins is provided by interfaces in Java like the Comparable functionality for instance. However,we know that an interface only specifies what the class must support and cannot provide an implementation unlike a module which can be mixed into any number of classes. For refactoring common behavior into a single place in java (to achieve the power of mixins),we need another class which provides an implementation and is dependent on the interface.It has been observed that Interfaces when combined with aspect-oriented programming can produce full fledged mixins in Java.

References:

<references/>