<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vmallad</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Vmallad"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vmallad"/>
	<updated>2026-06-08T17:32:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55110</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55110"/>
		<updated>2011-11-16T02:57:54Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''“If you promise to call m with pre satisfied then I, in return, promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;''&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in [http://en.wikipedia.org/wiki/Software Software System]- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to a Software System as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/Programmer Programmer] must provide the user with a valid outcome(obligation) and can expect that the [http://en.wikipedia.org/wiki/User_(computing) User] has given a correct and valid input(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/User_(computing) User] must provide a valid input(obligation) to a method and can expect the [http://en.wikipedia.org/wiki/Programmer Programmer] to give the correct output(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Some properties such as stack size discussed earlier are satisfied by the system at all points of time.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, it is not the responsibility of the Programmer to satisfy the pre-condition. In fact, if the pre-condition is not satisfied by the user, the programmer need not have to do anything. Once the pre-condition is failed, it is not the Programmers' fault even if the system crashes or gives blunders as results.&lt;br /&gt;
&lt;br /&gt;
In order to gain an insight about how programming By Contract actually works, consider the webAssign example from our class:&lt;br /&gt;
&lt;br /&gt;
All the students are supposed to give the tests through WebAssign. During the test process, each student is expected to write their answers in the designated text boxes and save the test no more than 4 times. This is as an obligation for the student. The student in turn is ensured that all his answers are correctly recorded and rendered to the professor for evaluation. This is a benefit for the student.&lt;br /&gt;
&lt;br /&gt;
WebAssign is supposed to record the answers given by the student appropriately and render them to the professor for evaluation. This is an obligation for WebAssign. WebAssign is entitled to ensure that the user has answered in the designated text boxes and saved no more than 4 times. This is a benefit to WebAssign.&lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
In JAVA, although the exact Programming by contract facility is not provided, it provides an informal &amp;quot;similar&amp;quot; kind of functionality through Programming By Assertions.&lt;br /&gt;
&lt;br /&gt;
===Assertions in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;An [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a statement in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language that enables you to test your assumptions about your program.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each assertion contains a [http://en.wikipedia.org/wiki/Boolean_function boolean] expression which we believe should be true. If the boolean expression does not return true, the program throws an error and the further execution of the code is terminated. &lt;br /&gt;
&lt;br /&gt;
There are two forms of assertions in Java:&lt;br /&gt;
&lt;br /&gt;
1. assert Expression;&lt;br /&gt;
Here, assert is a keyword and Expression is a Boolean expression. If the expression returns true, the method execution is continued. If the expression returns false, then an AssertionError with no much detail is thrown and the method execution is terminated.&lt;br /&gt;
&lt;br /&gt;
2. assert Expression1 :Expression2;&lt;br /&gt;
Here, we use assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor], which uses the string representation of the value as the error's detail message.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&amp;lt;pre&amp;gt;assert !true : &amp;quot;assert is not true&amp;quot;;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError:assert is not true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
Consider the following requirement:&lt;br /&gt;
&lt;br /&gt;
The job is to pour water in a glass. The method has to pour water in the glass assuming that glass is not full. This is a precondition and can be checked using a assert statement. After the method is called, the amount of water in the class should be greater than before. This is a postcondition and can be checked by a asseret statement. At all times, the water level should be maximum if the glass is full and should be less than maximum if the glass is not full. This is Class Invariant and is checked through a method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class WaterGlass{&lt;br /&gt;
	boolean isFull;&lt;br /&gt;
	int water_level;&lt;br /&gt;
	final int max_level= 10;&lt;br /&gt;
	WaterGlass()&lt;br /&gt;
	{&lt;br /&gt;
		isFull = false;&lt;br /&gt;
		water_level = 0;&lt;br /&gt;
	}&lt;br /&gt;
	// this is the class invariant. This always returns true&lt;br /&gt;
	public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
	//this is the method pourWater&lt;br /&gt;
	public void pourWater()&lt;br /&gt;
	{&lt;br /&gt;
			// Pre Condition. we check that the glass is not full before going into the method.&lt;br /&gt;
			// If the glass is full, we print the message and stop the function execution.&lt;br /&gt;
			assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
	&lt;br /&gt;
			int prev_level = this.water_level;&lt;br /&gt;
			this.water_level= this.water_level+1;&lt;br /&gt;
			if(this.water_level == max_level)&lt;br /&gt;
			{&lt;br /&gt;
				isFull=true;&lt;br /&gt;
			}&lt;br /&gt;
			System.out.println(&amp;quot;the water level is &amp;quot; + this.water_level);&lt;br /&gt;
			&lt;br /&gt;
			// Post Condition. we check that whenever the pre condition is met, &lt;br /&gt;
			// the new water level is always greater than the original water level&lt;br /&gt;
			&lt;br /&gt;
			assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
	}&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	{&lt;br /&gt;
		WaterGlass myGlass=new WaterGlass();&lt;br /&gt;
		&lt;br /&gt;
		for(int i=0;i&amp;lt;=10;i++)&lt;br /&gt;
		{&lt;br /&gt;
			myGlass.pourWater();&lt;br /&gt;
			System.out.println(&amp;quot;invariant is &amp;quot; + myGlass.invariant());&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of this code is :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
the water level is 1&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 2&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 3&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 4&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 5&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 6&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 7&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 8&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 9&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 10&lt;br /&gt;
invariant is true&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Glass is already full. cant fill in more water&lt;br /&gt;
	at WaterGlass.pourWater(WaterGlass.java:22)&lt;br /&gt;
	at WaterGlass.main(WaterGlass.java:43)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a way of implementing programming By Contract in Java.&lt;br /&gt;
&lt;br /&gt;
Pre-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the glass is already full. If it is not full, the method executes. If it is full, the execution terminates.&lt;br /&gt;
&lt;br /&gt;
Post-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the level of water is greater than the previous level. This should always be true given the pre-Condition is satisfied.&lt;br /&gt;
&lt;br /&gt;
Class Invariant: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks that at any point of time during the execution of the class, if the glass is full, the water level should be maximum. If the glass is not full, the water level should be less than the maximum level.&lt;br /&gt;
&lt;br /&gt;
This is an &amp;quot;almost similar&amp;quot; approach to Programming By Contract. When asked about why Java does not provide a full-fledged design-by-contract facility with preconditions, postconditions and class invariants, like the one in the Eiffel programming language, the answer is&lt;br /&gt;
&lt;br /&gt;
&amp;quot;We considered providing such a facility, but were unable to convince ourselves that it is possible to graft it onto the Java programming language without massive changes to the Java platform libraries, and massive inconsistencies between old and new libraries. Further, we were not convinced that such a facility would preserve the simplicity that is the hallmark of the Java programming language. On balance, we came to the conclusion that a simple boolean assertion facility was a fairly straight-forward solution and far less risky. It's worth noting that adding a boolean assertion facility to the language doesn't preclude adding a full-fledged design-by-contract facility at some time in the future.&lt;br /&gt;
&lt;br /&gt;
The simple assertion facility does enable a limited form of design-by-contract style programming. The assert statement is appropriate for nonpublic precondition, postcondition and class invariant checking. Public precondition checking should still be performed by checks inside methods that result in particular, documented exceptions, such as IllegalArgumentException and IllegalStateException.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#design-faq&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
To the programmers, Programming By Contract guarantees that [http://en.wikipedia.org/wiki/Software_bug bugs] will be prevented by a well defined mechanism.This is based on checks for pre-conditions. For customers, it offers a label of quality, seriousness, and a job well done.&lt;br /&gt;
&lt;br /&gt;
To meet the needs of today's large-scale and mission critical applications, this unique solution is a must.&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55109</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55109"/>
		<updated>2011-11-16T02:57:39Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''“If you promise to call m with pre satisfied then I, in return, promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;''&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in [http://en.wikipedia.org/wiki/Software Software System]- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to a Software System as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/Programmer Programmer] must provide the user with a valid outcome(obligation) and can expect that the [http://en.wikipedia.org/wiki/User_(computing) User] has given a correct and valid input(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/User_(computing) User] must provide a valid input(obligation) to a method and can expect the [http://en.wikipedia.org/wiki/Programmer Programmer] to give the correct output(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Some properties such as stack size discussed earlier are satisfied by the system at all points of time.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, it is not the responsibility of the Programmer to satisfy the pre-condition. In fact, if the pre-condition is not satisfied by the user, the programmer need not have to do anything. Once the pre-condition is failed, it is not the Programmers' fault even if the system crashes or gives blunders as results.&lt;br /&gt;
&lt;br /&gt;
In order to gain an insight about how programming By Contract actually works, consider the webAssign example from our class:&lt;br /&gt;
&lt;br /&gt;
All the students are supposed to give the tests through WebAssign. During the test process, each student is expected to write their answers in the designated text boxes and save the test no more than 4 times. This is as an obligation for the student. The student in turn is ensured that all his answers are correctly recorded and rendered to the professor for evaluation. This is a benefit for the student.&lt;br /&gt;
&lt;br /&gt;
WebAssign is supposed to record the answers given by the student appropriately and render them to the professor for evaluation. This is an obligation for WebAssign. WebAssign is entitled to ensure that the user has answered in the designated text boxes and saved no more than 4 times. This is a benefit to WebAssign.&lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
In JAVA, although the exact Programming by contract facility is not provided, it provides an informal &amp;quot;similar&amp;quot; kind of functionality through Programming By Assertions.&lt;br /&gt;
&lt;br /&gt;
===Assertions in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;An [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a statement in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language that enables you to test your assumptions about your program.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each assertion contains a [http://en.wikipedia.org/wiki/Boolean_function boolean] expression which we believe should be true. If the boolean expression does not return true, the program throws an error and the further execution of the code is terminated. &lt;br /&gt;
&lt;br /&gt;
There are two forms of assertions in Java:&lt;br /&gt;
&lt;br /&gt;
1. assert Expression;&lt;br /&gt;
Here, assert is a keyword and Expression is a Boolean expression. If the expression returns true, the method execution is continued. If the expression returns false, then an AssertionError with no much detail is thrown and the method execution is terminated.&lt;br /&gt;
&lt;br /&gt;
2. assert Expression1 :Expression2;&lt;br /&gt;
Here, we use assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor], which uses the string representation of the value as the error's detail message.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&amp;lt;pre&amp;gt;assert !true : &amp;quot;assert is not true&amp;quot;;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError:assert is not true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
Consider the following requirement:&lt;br /&gt;
&lt;br /&gt;
The job is to pour water in a glass. The method has to pour water in the glass assuming that glass is not full. This is a precondition and can be checked using a assert statement. After the method is called, the amount of water in the class should be greater than before. This is a postcondition and can be checked by a asseret statement. At all times, the water level should be maximum if the glass is full and should be less than maximum if the glass is not full. This is Class Invariant and is checked through a method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class WaterGlass{&lt;br /&gt;
	boolean isFull;&lt;br /&gt;
	int water_level;&lt;br /&gt;
	final int max_level= 10;&lt;br /&gt;
	WaterGlass()&lt;br /&gt;
	{&lt;br /&gt;
		isFull = false;&lt;br /&gt;
		water_level = 0;&lt;br /&gt;
	}&lt;br /&gt;
	// this is the class invariant. This always returns true&lt;br /&gt;
	public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
	//this is the method pourWater&lt;br /&gt;
	public void pourWater()&lt;br /&gt;
	{&lt;br /&gt;
			// Pre Condition. we check that the glass is not full before going into the method.&lt;br /&gt;
			// If the glass is full, we print the message and stop the function execution.&lt;br /&gt;
			assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
	&lt;br /&gt;
			int prev_level = this.water_level;&lt;br /&gt;
			this.water_level= this.water_level+1;&lt;br /&gt;
			if(this.water_level == max_level)&lt;br /&gt;
			{&lt;br /&gt;
				isFull=true;&lt;br /&gt;
			}&lt;br /&gt;
			System.out.println(&amp;quot;the water level is &amp;quot; + this.water_level);&lt;br /&gt;
			&lt;br /&gt;
			// Post Condition. we check that whenever the pre condition is met, &lt;br /&gt;
			// the new water level is always greater than the original water level&lt;br /&gt;
			&lt;br /&gt;
			assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
	}&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	{&lt;br /&gt;
		WaterGlass myGlass=new WaterGlass();&lt;br /&gt;
		&lt;br /&gt;
		for(int i=0;i&amp;lt;=10;i++)&lt;br /&gt;
		{&lt;br /&gt;
			myGlass.pourWater();&lt;br /&gt;
			System.out.println(&amp;quot;invariant is &amp;quot; + myGlass.invariant());&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of this code is :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
the water level is 1&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 2&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 3&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 4&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 5&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 6&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 7&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 8&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 9&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 10&lt;br /&gt;
invariant is true&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Glass is already full. cant fill in more water&lt;br /&gt;
	at WaterGlass.pourWater(WaterGlass.java:22)&lt;br /&gt;
	at WaterGlass.main(WaterGlass.java:43)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a way of implementing programming By Contract in Java.&lt;br /&gt;
&lt;br /&gt;
Pre-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the glass is already full. If it is not full, the method executes. If it is full, the execution terminates.&lt;br /&gt;
&lt;br /&gt;
Post-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the level of water is greater than the previous level. This should always be true given the pre-Condition is satisfied.&lt;br /&gt;
&lt;br /&gt;
Class Invariant: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks that at any point of time during the execution of the class, if the glass is full, the water level should be maximum. If the glass is not full, the water level should be less than the maximum level.&lt;br /&gt;
&lt;br /&gt;
This is an &amp;quot;almost similar&amp;quot; approach to Programming By Contract. When asked about why Java does not provide a full-fledged design-by-contract facility with preconditions, postconditions and class invariants, like the one in the Eiffel programming language, the answer is&lt;br /&gt;
&lt;br /&gt;
&amp;quot;We considered providing such a facility, but were unable to convince ourselves that it is possible to graft it onto the Java programming language without massive changes to the Java platform libraries, and massive inconsistencies between old and new libraries. Further, we were not convinced that such a facility would preserve the simplicity that is the hallmark of the Java programming language. On balance, we came to the conclusion that a simple boolean assertion facility was a fairly straight-forward solution and far less risky. It's worth noting that adding a boolean assertion facility to the language doesn't preclude adding a full-fledged design-by-contract facility at some time in the future.&lt;br /&gt;
&lt;br /&gt;
The simple assertion facility does enable a limited form of design-by-contract style programming. The assert statement is appropriate for nonpublic precondition, postcondition and class invariant checking. Public precondition checking should still be performed by checks inside methods that result in particular, documented exceptions, such as IllegalArgumentException and IllegalStateException.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#design-faq&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
To the programmers, Programming By Contract guarantees that [http://en.wikipedia.org/wiki/Software_bug bugs] will be prevented by a well defined mechanism.This is based on checks for pre-conditions. For customers, it offers a label of quality, seriousness, and a job well done.&lt;br /&gt;
&lt;br /&gt;
To meet the needs of today's large-scale and mission critical applications, this unique solution is a must.&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55107</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55107"/>
		<updated>2011-11-16T02:55:54Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''“If you promise to call m with pre satisfied then I, in return, promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;''&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in [http://en.wikipedia.org/wiki/Software Software System]- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to a Software System as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/Programmer Programmer] must provide the user with a valid outcome(obligation) and can expect that the [http://en.wikipedia.org/wiki/User_(computing) User] has given a correct and valid input(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/User_(computing) User] must provide a valid input(obligation) to a method and can expect the [http://en.wikipedia.org/wiki/Programmer Programmer] to give the correct output(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Some properties such as stack size discussed earlier are satisfied by the system at all points of time.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, it is not the responsibility of the Programmer to satisfy the pre-condition. In fact, if the pre-condition is not satisfied by the user, the programmer need not have to do anything. Once the pre-condition is failed, it is not the Programmers' fault even if the system crashes or gives blunders as results.&lt;br /&gt;
&lt;br /&gt;
In order to gain an insight about how programming By Contract actually works, consider the webAssign example from our class:&lt;br /&gt;
&lt;br /&gt;
All the students are supposed to give the tests through WebAssign. During the test process, each student is expected to write their answers in the designated text boxes and save the test no more than 4 times. This is as an obligation for the student. The student in turn is ensured that all his answers are correctly recorded and rendered to the professor for evaluation. This is a benefit for the student.&lt;br /&gt;
&lt;br /&gt;
WebAssign is supposed to record the answers given by the student appropriately and render them to the professor for evaluation. This is an obligation for WebAssign. WebAssign is entitled to ensure that the user has answered in the designated text boxes and saved no more than 4 times. This is a benefit to WebAssign.&lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
In JAVA, although the exact Programming by contract facility is not provided, it provides an informal &amp;quot;similar&amp;quot; kind of functionality through Programming By Assertions.&lt;br /&gt;
&lt;br /&gt;
===Assertions in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;An [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a statement in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language that enables you to test your assumptions about your program.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each assertion contains a [http://en.wikipedia.org/wiki/Boolean_function boolean] expression which we believe should be true. If the boolean expression does not return true, the program throws an error and the further execution of the code is terminated. &lt;br /&gt;
&lt;br /&gt;
There are two forms of assertions in Java:&lt;br /&gt;
&lt;br /&gt;
1. assert Expression;&lt;br /&gt;
Here, assert is a keyword and Expression is a Boolean expression. If the expression returns true, the method execution is continued. If the expression returns false, then an AssertionError with no much detail is thrown and the method execution is terminated.&lt;br /&gt;
&lt;br /&gt;
2. assert Expression1 :Expression2;&lt;br /&gt;
Here, we use assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor], which uses the string representation of the value as the error's detail message.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&amp;lt;pre&amp;gt;assert !true : &amp;quot;assert is not true&amp;quot;;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError:assert is not true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
Consider the following requirement:&lt;br /&gt;
&lt;br /&gt;
The job is to pour water in a glass. The method has to pour water in the glass assuming that glass is not full. This is a precondition and can be checked using a assert statement. After the method is called, the amount of water in the class should be greater than before. This is a postcondition and can be checked by a asseret statement. At all times, the water level should be maximum if the glass is full and should be less than maximum if the glass is not full. This is Class Invariant and is checked through a method.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class WaterGlass{&lt;br /&gt;
	boolean isFull;&lt;br /&gt;
	int water_level;&lt;br /&gt;
	final int max_level= 10;&lt;br /&gt;
	WaterGlass()&lt;br /&gt;
	{&lt;br /&gt;
		isFull = false;&lt;br /&gt;
		water_level = 0;&lt;br /&gt;
	}&lt;br /&gt;
	// this is the class invariant. This always returns true&lt;br /&gt;
	public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
	//this is the method pourWater&lt;br /&gt;
	public void pourWater()&lt;br /&gt;
	{&lt;br /&gt;
			// Pre Condition. we check that the glass is not full before going into the method.&lt;br /&gt;
			// If the glass is full, we print the message and stop the function execution.&lt;br /&gt;
			assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
	&lt;br /&gt;
			int prev_level = this.water_level;&lt;br /&gt;
			this.water_level= this.water_level+1;&lt;br /&gt;
			if(this.water_level == max_level)&lt;br /&gt;
			{&lt;br /&gt;
				isFull=true;&lt;br /&gt;
			}&lt;br /&gt;
			System.out.println(&amp;quot;the water level is &amp;quot; + this.water_level);&lt;br /&gt;
			&lt;br /&gt;
			// Post Condition. we check that whenever the pre condition is met, &lt;br /&gt;
			// the new water level is always greater than the original water level&lt;br /&gt;
			&lt;br /&gt;
			assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
	}&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	{&lt;br /&gt;
		WaterGlass myGlass=new WaterGlass();&lt;br /&gt;
		&lt;br /&gt;
		for(int i=0;i&amp;lt;=10;i++)&lt;br /&gt;
		{&lt;br /&gt;
			myGlass.pourWater();&lt;br /&gt;
			System.out.println(&amp;quot;invariant is &amp;quot; + myGlass.invariant());&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output of this code is :&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
the water level is 1&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 2&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 3&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 4&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 5&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 6&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 7&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 8&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 9&lt;br /&gt;
invariant is true&lt;br /&gt;
the water level is 10&lt;br /&gt;
invariant is true&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError: Glass is already full. cant fill in more water&lt;br /&gt;
	at WaterGlass.pourWater(WaterGlass.java:22)&lt;br /&gt;
	at WaterGlass.main(WaterGlass.java:43)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This is a way of implementing programming By Contract in Java.&lt;br /&gt;
&lt;br /&gt;
Pre-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the glass is already full. If it is not full, the method executes. If it is full, the execution terminates.&lt;br /&gt;
&lt;br /&gt;
Post-Condition Check:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks if the level of water is greater than the previous level. This should always be true given the pre-Condition is satisfied.&lt;br /&gt;
&lt;br /&gt;
Class Invariant: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This checks that at any point of time during the execution of the class, if the glass is full, the water level should be maximum. If the glass is not full, the water level should be less than the maximum level.&lt;br /&gt;
&lt;br /&gt;
This is an &amp;quot;almost similar&amp;quot; approach to Programming By Contract. When asked about why Java does not provide a full-fledged design-by-contract facility with preconditions, postconditions and class invariants, like the one in the Eiffel programming language, the answer is&lt;br /&gt;
&lt;br /&gt;
&amp;quot;We considered providing such a facility, but were unable to convince ourselves that it is possible to graft it onto the Java programming language without massive changes to the Java platform libraries, and massive inconsistencies between old and new libraries. Further, we were not convinced that such a facility would preserve the simplicity that is the hallmark of the Java programming language. On balance, we came to the conclusion that a simple boolean assertion facility was a fairly straight-forward solution and far less risky. It's worth noting that adding a boolean assertion facility to the language doesn't preclude adding a full-fledged design-by-contract facility at some time in the future.&lt;br /&gt;
&lt;br /&gt;
The simple assertion facility does enable a limited form of design-by-contract style programming. The assert statement is appropriate for nonpublic precondition, postcondition and class invariant checking. Public precondition checking should still be performed by checks inside methods that result in particular, documented exceptions, such as IllegalArgumentException and IllegalStateException.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#design-faq&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
&lt;br /&gt;
To the programmers, Programming By Contract guarantees that [http://en.wikipedia.org/wiki/Software_bug bugs] will be prevented by a well defined mechanism.This is based on checks for pre-conditions. For customers, it offers a label of quality, seriousness, and a job well done.&lt;br /&gt;
&lt;br /&gt;
To meet the needs of today's large-scale and mission critical applications, this unique solution is a must.&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55104</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55104"/>
		<updated>2011-11-16T02:32:49Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''“If you promise to call m with pre satisfied then I, in return, promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;''&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in [http://en.wikipedia.org/wiki/Software Software System]- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to a Software System as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/Programmer Programmer] must provide the user with a valid outcome(obligation) and can expect that the [http://en.wikipedia.org/wiki/User_(computing) User] has given a correct and valid input(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The [http://en.wikipedia.org/wiki/User_(computing) User] must provide a valid input(obligation) to a method and can expect the [http://en.wikipedia.org/wiki/Programmer Programmer] to give the correct output(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Some properties such as stack size discussed earlier are satisfied by the system at all points of time.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, it is not the responsibility of the Programmer to satisfy the pre-condition. In fact, if the pre-condition is not satisfied by the user, the programmer need not have to do anything. Once the pre-condition is failed, it is not the Programmers' fault even if the system crashes or gives blunders as results.&lt;br /&gt;
&lt;br /&gt;
In order to gain an insight about how programming By Contract actually works, consider the webAssign example from our class:&lt;br /&gt;
&lt;br /&gt;
All the students are supposed to give the tests through WebAssign. During the test process, each student is expected to write their answers in the designated text boxes and save the test no more than 4 times. This is as an obligation for the student. The student in turn is ensured that all his answers are correctly recorded and rendered to the professor for evaluation. This is a benefit for the student.&lt;br /&gt;
&lt;br /&gt;
WebAssign is supposed to record the answers given by the student appropriately and render them to the professor for evaluation. This is an obligation for WebAssign. WebAssign is entitled to ensure that the user has answered in the designated text boxes and saved no more than 4 times. This is a benefit to WebAssign.&lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
In JAVA, although the exact Programming by contract facility is not provided, it provides an informal &amp;quot;similar&amp;quot; kind of functionality through Programming By Assertions.&lt;br /&gt;
&lt;br /&gt;
===Assertions in Java===&lt;br /&gt;
&lt;br /&gt;
&amp;quot;An [http://en.wikipedia.org/wiki/Assertion_(computing) assertion] is a statement in the [http://en.wikipedia.org/wiki/Java_(programming_language) Java] programming language that enables you to test your assumptions about your program.&amp;quot;&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Each assertion contains a [http://en.wikipedia.org/wiki/Boolean_function boolean] expression which we believe should be true. If the boolean expression does not return true, the program throws an error and the further execution of the code is terminated. &lt;br /&gt;
&lt;br /&gt;
There are two forms of assertions in Java:&lt;br /&gt;
&lt;br /&gt;
1. assert Expression;&lt;br /&gt;
Here, assert is a keyword and Expression is a Boolean expression. If the expression returns true, the method execution is continued. If the expression returns false, then an AssertionError with no much detail is thrown and the method execution is terminated.&lt;br /&gt;
&lt;br /&gt;
2. assert Expression1 :Expression2;&lt;br /&gt;
Here, we use assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError [http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming) constructor], which uses the string representation of the value as the error's detail message.&lt;br /&gt;
&lt;br /&gt;
e.g.&lt;br /&gt;
&amp;lt;pre&amp;gt;assert !true : &amp;quot;assert is not true&amp;quot;;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The output will be:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Exception in thread &amp;quot;main&amp;quot; java.lang.AssertionError:assert is not true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
Consider the following requirement:&lt;br /&gt;
&lt;br /&gt;
The job is to pour water in a glass. The method has to pour water in the glass assuming that glass is not full. This is a precondition and can be checked using a assert statement. After the method is called, the amount of water in the class should be greater than before. This is a postcondition and can be checked by a asseret statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class WaterGlass{&lt;br /&gt;
	boolean isFull;&lt;br /&gt;
	int water_level;&lt;br /&gt;
	final int max_level= 10;&lt;br /&gt;
	WaterGlass()&lt;br /&gt;
	{&lt;br /&gt;
		isFull = false;&lt;br /&gt;
		water_level = 0;&lt;br /&gt;
	}&lt;br /&gt;
	// this is the class invariant. This always returns true&lt;br /&gt;
	public boolean invariant()&lt;br /&gt;
	{&lt;br /&gt;
		&lt;br /&gt;
		return ( (this.isFull == true &amp;amp;&amp;amp; water_level==max_level)|| ( this.isFull == false &amp;amp;&amp;amp; water_level&amp;lt;max_level));&lt;br /&gt;
				 &lt;br /&gt;
	}&lt;br /&gt;
	//this is the method pourWater&lt;br /&gt;
	public void pourWater()&lt;br /&gt;
	{&lt;br /&gt;
			// Pre Condition. we check that the glass is not full before going into the method.&lt;br /&gt;
			// If the glass is full, we print the message and stop the function execution.&lt;br /&gt;
			assert !this.isFull : &amp;quot;Glass is already full. cant fill in more water&amp;quot;;&lt;br /&gt;
	&lt;br /&gt;
			int prev_level = this.water_level;&lt;br /&gt;
			this.water_level= this.water_level+1;&lt;br /&gt;
			if(this.water_level == max_level)&lt;br /&gt;
			{&lt;br /&gt;
				isFull=true;&lt;br /&gt;
			}&lt;br /&gt;
			System.out.println(&amp;quot;the water level is &amp;quot; + this.water_level);&lt;br /&gt;
			&lt;br /&gt;
			// Post Condition. we check that whenever the pre condition is met, &lt;br /&gt;
			// the new water level is always greater than the original water level&lt;br /&gt;
			&lt;br /&gt;
			assert this.water_level&amp;gt;prev_level;&lt;br /&gt;
	}&lt;br /&gt;
	public static void main(String[] args)&lt;br /&gt;
	{&lt;br /&gt;
		WaterGlass myGlass=new WaterGlass();&lt;br /&gt;
		&lt;br /&gt;
		for(int i=0;i&amp;lt;=10;i++)&lt;br /&gt;
		{&lt;br /&gt;
			myGlass.pourWater();&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55092</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55092"/>
		<updated>2011-11-16T01:53:08Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''&lt;br /&gt;
“If you promise to call m with pre satisfied then I, in return, &lt;br /&gt;
promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&lt;br /&gt;
&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in software system- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to a Software System as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The programmer must provide the user with a valid outcome(obligation) and can expect that the user has given a correct and valid input(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The User must provide a valid input(obligation) to a method and can expect the programmer to give the correct output(benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Some properties such as stack size discussed earlier are satisfied by the system at all points of time.&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In order to gain an insight about how programming By Contract actually works, consider the webAssign example from our class:&lt;br /&gt;
&lt;br /&gt;
All the students are supposed to give the tests through WebAssign. During the test process, each student is expected to write their answers in the designated text boxes and save the test no more than 4 times. This is as an obligation for the student. The student in turn is ensured that all his answers are correctly recorded and rendered to the professor for evaluation. This is a benefit for the student.&lt;br /&gt;
&lt;br /&gt;
WebAssign is supposed to record the answers given by the student appropriately and render them to the professor for evaluation. This is an obligation for WebAssign. WebAssign is entitled to ensure that the user has answered in the designated text boxes and saved no more than 4 times. This is a benefit to WebAssign.&lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55091</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55091"/>
		<updated>2011-11-16T01:31:15Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''&lt;br /&gt;
“If you promise to call m with pre satisfied then I, in return, &lt;br /&gt;
promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&lt;br /&gt;
&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in software system- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to software as below&lt;br /&gt;
* The User must provide a &lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
===Examples===&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55090</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55090"/>
		<updated>2011-11-16T01:30:23Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Class_invariant class invariant] is a predicate that must be true at all points during a program. This is should hold no matter what a method does in the [http://en.wikipedia.org/wiki/Class_(computer_programming) class].&lt;br /&gt;
&lt;br /&gt;
Consider a stack. In a stack, the size should always be greater than 0 and less than the capacity. This should always hold. This property of a [http://en.wikipedia.org/wiki/Stack_(data_structure) stack] can neither be represented by a pre-condition nor by a post-condition. Such properties of a class are called as Class invariants.&lt;br /&gt;
&lt;br /&gt;
The useful effect of class invariants in [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented software] is enhanced in the presence of [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) inheritance]. Class invariants are inherited, that is, &amp;quot;the invariants of all the parents of a class apply to the class itself.&amp;quot;&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Programming By Contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is known under the name of Design by Contract™ first implemented by [http://en.wikipedia.org/wiki/Eiffel_(programming_language) Eiffel], a programming language introduced by [http://en.wikipedia.org/wiki/Bertrand_Meyer Bertrand Meyer].It provides the programmers a way to verify that the execution of their methods does not corrupt the state of the data structures.&lt;br /&gt;
&lt;br /&gt;
===Definition===&lt;br /&gt;
&lt;br /&gt;
According to Bertrand Meyer, &lt;br /&gt;
&lt;br /&gt;
By associating pre- and postcondition assertions with a method m,the class tells its clients1&lt;br /&gt;
''&lt;br /&gt;
“If you promise to call m with pre satisfied then I, in return, &lt;br /&gt;
promise to deliver a final state in which post is satisfied&amp;quot;''&amp;lt;ref&amp;gt; Meyer, Bertrand. Object-Oriented Software Construction, second edition, Prentice Hall, 1997, p. 570&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In other words,&lt;br /&gt;
''&lt;br /&gt;
&amp;quot;Basically programming by contract creates a contract between the software developer and software user - in Meyer's terms the supplier and the consumer. Every feature, or method, starts with a precondition that must be satisfied by the consumer of the routine. And each feature ends with postconditions which the supplier guarantees to be true (if and only if the preconditions were met). Also, each class has an invariant which must be satisfied after any changes to the object represented by the class. In the other words, the invariant guarantees the object is in a valid state.&amp;quot;&amp;lt;ref&amp;gt;http://www.cs.unc.edu/~stotts/COMP204/contract.html&amp;lt;/ref&amp;gt;''&lt;br /&gt;
&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
&lt;br /&gt;
There are two main elements in software system- User and Programmer. In order to understand Obligations and benefits correctly, let us first consider a metaphor and later apply that to Software. The metaphor comes from business life, where a &amp;quot;client&amp;quot; and a &amp;quot;supplier&amp;quot; agree on a &amp;quot;contract&amp;quot; which documents that:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#008080&amp;quot;&amp;gt;* The supplier must provide a certain product (obligation) and is entitled to expect that the client has paid its fee (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#A52A2A&amp;quot;&amp;gt;* The client must pay the fee (obligation) and is entitled to get the product (benefit).&amp;lt;/span&amp;gt;&lt;br /&gt;
&amp;lt;span style=&amp;quot;color:#483D8B&amp;quot;&amp;gt;* Both parties must satisfy certain obligations, such as laws and regulations, applying to all contracts.&amp;lt;/span&amp;gt;&amp;lt;ref&amp;gt;http://www.eiffel.com/developers/design_by_contract.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The metaphor relates to software as below&lt;br /&gt;
* The User must provide a &lt;br /&gt;
&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
===Examples===&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55089</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55089"/>
		<updated>2011-11-16T00:35:18Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
==Assertions==&lt;br /&gt;
===Pre-Conditions===&lt;br /&gt;
===Post-Conditions===&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
==Programming by contract==&lt;br /&gt;
===Definition===&lt;br /&gt;
===Obligations and benefits===&lt;br /&gt;
==Programming 'By Contract' in JAVA==&lt;br /&gt;
===Examples===&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55035</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55035"/>
		<updated>2011-11-15T18:45:21Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
==Writing robust code==&lt;br /&gt;
===Assertions===&lt;br /&gt;
===Exceptions===&lt;br /&gt;
===Programming by contract===&lt;br /&gt;
==Assertions==&lt;br /&gt;
==Class Invariants==&lt;br /&gt;
==Programming 'By Contract'==&lt;br /&gt;
===Definition===&lt;br /&gt;
===Obligations and benifits===&lt;br /&gt;
===Examples===&lt;br /&gt;
==Conclusion==&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=55009</id>
		<title>CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011&amp;diff=55009"/>
		<updated>2011-11-15T06:52:32Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Link title]]&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a cs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a ri]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1b tj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c cm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1c ka]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d sr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e vs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1a sc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e an]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e lm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g vn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1f sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g jn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1e sm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i zf]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1g rn]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1i lj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1h hs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 1d gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b ns]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b jp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a av]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f jm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ad]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e kt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e gp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b qu]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c bs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2c rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2a ca]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch1 2b rv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2c ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2b sa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f mm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2f vh]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch2 2e ps]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3a oe]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h rr]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 3h ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch3 4b ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4b ds]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i aa]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i sd]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d mt]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ls]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4d ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c ap]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h sv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e cl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4e gs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4a ga]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f sl]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i js]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f ss]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4c dm]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g nv]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4g ms]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h kp]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4h as]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4j fw]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4f rs]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch4 4i lc]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch17 5b uo]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch17 5b br]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch5 5d he]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch5 6d ny]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch6 6d sk]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch6 6e ch]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch6 6b ra]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch6 6c sj]]&lt;br /&gt;
&lt;br /&gt;
*[[CSC/ECE 517 Fall 2011/ch6 6a am]]&lt;br /&gt;
&lt;br /&gt;
*[[trial]]&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2011&amp;diff=55007</id>
		<title>Talk:CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2011&amp;diff=55007"/>
		<updated>2011-11-15T06:47:56Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: Blanked the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55006</id>
		<title>CSC/ECE 517 Fall 2011/ch6 6a am</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch6_6a_am&amp;diff=55006"/>
		<updated>2011-11-15T06:43:55Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: Created page with &amp;quot;test&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;test&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54999</id>
		<title>User:Vmallad</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54999"/>
		<updated>2011-11-15T06:33:51Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: CSC/ECE 517 Fall 2011/ch6 6a am&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; 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&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' 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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54994</id>
		<title>User:Vmallad</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54994"/>
		<updated>2011-11-15T06:28:09Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: Blanked the page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54993</id>
		<title>User:Vmallad</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Vmallad&amp;diff=54993"/>
		<updated>2011-11-15T06:27:42Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: asdf&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; 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&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' 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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2011&amp;diff=54992</id>
		<title>Talk:CSC/ECE 517 Fall 2011</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2011&amp;diff=54992"/>
		<updated>2011-11-15T06:24:50Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: /* ch6 6a am */ new section&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== ch6 6a am ==&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; 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&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a need to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable behavior in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second) #implementation of &amp;lt;=&amp;gt;&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue # we use the &amp;lt;=&amp;gt; of FixNum&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort #The elements(objects of Organisation) of the array a are sorted in the increasing order of their revenues.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Consider the same scenario as in the above example.The implementation in java is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''  Mix in Enumerable in a class as follows'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*inject &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in the string &amp;quot;123Bond007&amp;quot; are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. For example, to print all elements of a vector v:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
myrecipee.save #To insert into the DB&lt;br /&gt;
&lt;br /&gt;
myrecipee.Description=’Tasty and Delicious!’ #to modify the DB&lt;br /&gt;
 &lt;br /&gt;
myrecipee.destroy #to delete the tuple in the database&lt;br /&gt;
&lt;br /&gt;
puts myrecipee.inspect &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' 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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51635</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51635"/>
		<updated>2011-10-01T00:51:07Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;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. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module&amp;lt;ref&amp;gt;http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/ref&amp;gt; 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&amp;lt;ref&amp;gt;http://ruby-doc.org/&amp;lt;/ref&amp;gt;:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Mixin&amp;lt;/ref&amp;gt; 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
Following is a brief introduction of the predefined mixin modules.&lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method 'between?'. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the implementation for the method 'each'. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class&amp;lt;ref&amp;gt;http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/ref&amp;gt;. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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&amp;lt;ref&amp;gt;http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/ref&amp;gt;.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate&amp;lt;ref&amp;gt;http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/ref&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering software engineering], the singleton pattern&amp;lt;ref&amp;gt;http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/ref&amp;gt; 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior&amp;lt;ref&amp;gt;http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/ref&amp;gt; can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51632</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51632"/>
		<updated>2011-10-01T00:41:11Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51631</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51631"/>
		<updated>2011-10-01T00:39:34Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51629</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51629"/>
		<updated>2011-10-01T00:37:41Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about  interfaces in  Java,  mixins in  Ruby and compare some of the functionalities offered by mixins  and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
An interface can be defined in the following manner.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Some benefits of Interfaces in Java'''&lt;br /&gt;
*Interfaces allow an object to play several roles.ie They can be used to  simulate multiple inheritance.&lt;br /&gt;
*They help the developer to design the skeleton behavior for classes.&lt;br /&gt;
*An interface can also be used for creating a class that can store application level constants.&lt;br /&gt;
*Interfaces are very useful when publishing [http://en.wikipedia.org/wiki/Application_programming_interface APIs]&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A  Module 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	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).&lt;br /&gt;
*	The syntax for defining a  module is different.&lt;br /&gt;
&lt;br /&gt;
A module can be defined as follows :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module &amp;lt;Module Name&amp;gt;&lt;br /&gt;
     #define methods&lt;br /&gt;
     #define classes&lt;br /&gt;
     #define constants&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example''': Consider a requirement where a person must bow to a good guy and hit the bad guy with a bow.&lt;br /&gt;
&lt;br /&gt;
The module 'GoodGuy' implements a method 'bow' and tells the caller to bow to the guy because he is a good guy.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Note: A module's method can be called by &amp;lt;Module name&amp;gt;.&amp;lt;method name&amp;gt; and module constants are referred as &amp;lt;Module_name&amp;gt;::&amp;lt;method name&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
class Actnow&lt;br /&gt;
    include 'GoodGuy'&lt;br /&gt;
    include 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
        BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note:&lt;br /&gt;
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 &amp;lt;module name&amp;gt;' 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.&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&amp;lt;ref&amp;gt;http://ruby.about.com/od/sinatra/a/datamapper.htm&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51592</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51592"/>
		<updated>2011-09-30T22:16:56Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about [http://en.wikipedia.org/wiki/Interface_(computing) interfaces] in [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Mixins mixins] in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] and compare some of the functionalities offered by mixins in Ruby and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Module_(programming) Module] 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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. 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.&lt;br /&gt;
&lt;br /&gt;
'''Examples of Modules:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
 require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules in the above example are classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality as above cannot be implemented through a class. Because to do so, we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides the 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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
The methods of the module do not belong to the class requiring it. In order to make a module's method the instance methods of our class, we have to use the ''include'' keyword instead of'' require''. However,  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.&lt;br /&gt;
Consider the same example as in modules. we now write a class to extend both the modules and call the same 'bow' method of the two modules from the object of our class.&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
    require 'GoodGuy'&lt;br /&gt;
    require 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
      BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when there is a requirement to compare few objects and maybe even sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider the same example as above and the implementation for java is provided below.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51591</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51591"/>
		<updated>2011-09-30T22:07:28Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about [http://en.wikipedia.org/wiki/Interface_(computing) interfaces] in [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Mixins mixins] in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] and compare some of the functionalities offered by mixins in Ruby and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
               public void draw()&lt;br /&gt;
               {&lt;br /&gt;
              	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
               }&lt;br /&gt;
               public void displayArea(float a,float b) &lt;br /&gt;
               { &lt;br /&gt;
                  System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
               }&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
            public class InfDemo&lt;br /&gt;
             {&lt;br /&gt;
               public static void main(String args[])&lt;br /&gt;
               {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
               }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Module_(programming) Module] 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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. 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.&lt;br /&gt;
&lt;br /&gt;
'''Examples of Modules:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
 require 'BadGuy'&lt;br /&gt;
 GoodGuy.bow&lt;br /&gt;
 BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules in the above example are classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality as above cannot be implemented through a class. Because to do so, we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides the 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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
The methods of the module do not belong to the class requiring it. In order to make a module's method the instance methods of our class, we have to use the ''include'' keyword instead of'' require''. However,  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
    require 'GoodGuy'&lt;br /&gt;
    require 'BadGuy'&lt;br /&gt;
    def Act(kind)&lt;br /&gt;
      if(kind=='good')&lt;br /&gt;
        GoodGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
      if (kind =='bad')&lt;br /&gt;
      BadGuy.bow&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when  there is a requirement  to compare few objects and maybe even  sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about Organizations. Suppose we have to sort different organizations as to which is greater by comparing the total revenue of each. The implementation in Java is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
       	        else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	        else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
	      Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
	      Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
              Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
	      Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
  	      Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
	      Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	      list1.add(org1);&lt;br /&gt;
              list1.add(org2);&lt;br /&gt;
              list1.add(org3);&lt;br /&gt;
 	      list1.add(org4);&lt;br /&gt;
	      list1.add(org5);&lt;br /&gt;
	      list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? etc only then a runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg: Consider the class 'A' which includes the 'Comparable' mixin.It defines a method 'hi'. It does not implement the &amp;lt;=&amp;gt; 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 &amp;lt;,&amp;gt;etc  are used because the &amp;lt;=&amp;gt; method has not been implemented.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 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.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51574</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51574"/>
		<updated>2011-09-30T20:10:20Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about [http://en.wikipedia.org/wiki/Interface_(computing) interfaces] in [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Mixins mixins] in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] and compare some of the functionalities offered by mixins in Ruby and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                    {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
                     }&lt;br /&gt;
&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
           public class InfDemo&lt;br /&gt;
           {&lt;br /&gt;
                 public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Module_(programming) Module] 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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. 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.&lt;br /&gt;
&lt;br /&gt;
'''Examples of Modules:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
  GoodGuy.bow&lt;br /&gt;
  BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules in the above example are classes. Ruby is [http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming) single-inherited], so the same functionality as above cannot be implemented through a class. Because to do so, we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides the 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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
The methods of the module do not belong to the class requiring it. In order to make a module's method the instance methods of our class, we have to use the ''include'' keyword instead of'' require''. However,  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
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], Preci-sion, [http://www.ruby-doc.org/core/classes/Process.html Process], [http://ruby-doc.org/core/classes/Signal.html Signal]. &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''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. &lt;br /&gt;
&lt;br /&gt;
'''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. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when  there is a requirement  to compare few objects and maybe even  sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about Organizations. Suppose we have to sort different organizations as to which is greater by comparing the total revenue of each. The implementation in Java is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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].&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Hibernate 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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
According to Wikipedia ,''” In [http://en.wikipedia.org/wiki/Software_engineering 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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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 nd providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
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].&lt;br /&gt;
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 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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:''' In this example,we define a module 'Wisher' which defines a method 'wish' which uses a method of 'name' of the host class ie the  class in which we are going to include this module. Next we define the host class (Person) which includes 'Wisher'. It 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. &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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 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.&lt;br /&gt;
&lt;br /&gt;
*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 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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 programming]  can produce full fledged mixins in  Java.&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51564</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=51564"/>
		<updated>2011-09-30T18:54:41Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Here we discuss about [http://en.wikipedia.org/wiki/Interface_(computing) interfaces] in [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Mixins mixins] in [http://en.wikipedia.org/wiki/Ruby_(programming_language) Ruby] and compare some of the functionalities offered by mixins in Ruby and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
      &amp;lt;method declarations&amp;gt;&lt;br /&gt;
    }&lt;br /&gt;
   &amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Contractual Obligation:'''&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of an Interface:''' &lt;br /&gt;
&lt;br /&gt;
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'  &lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
  &amp;lt;pre&amp;gt;&lt;br /&gt;
            interface Shape&lt;br /&gt;
                {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                }&lt;br /&gt;
            &lt;br /&gt;
            class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                    {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                    }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);&lt;br /&gt;
                     }&lt;br /&gt;
&lt;br /&gt;
             }&lt;br /&gt;
&lt;br /&gt;
           public class InfDemo&lt;br /&gt;
           {&lt;br /&gt;
                 public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A Module 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:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''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. 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.&lt;br /&gt;
&lt;br /&gt;
'''Examples of Modules:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
  def GoodGuy.bow&lt;br /&gt;
    puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
  def BadGuy.bow&lt;br /&gt;
     puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy' #require is used to load and execute the code once.&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
  GoodGuy.bow&lt;br /&gt;
  BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins===&lt;br /&gt;
Mixins 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time .''&lt;br /&gt;
&lt;br /&gt;
===Mixins===&lt;br /&gt;
Mixins are used to make certain behavior(s) available to a class.&lt;br /&gt;
&lt;br /&gt;
Lets assume  that the two modules in the above example are classes. Ruby is single-inherited, so the same functionality as above cannot be implemented through a class. Because to do so, we need  to extend both the GoodGuy class and the BadGuy class which is not possible. Through modules, Ruby provides the 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.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
The methods of the module do not belong to the class requiring it. In order to make a module's method the instance methods of our class, we have to use the ''include'' keyword instead of'' require''. However,  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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Some Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable Behavior ==&lt;br /&gt;
The comparable functionality is useful when  there is a requirement  to compare few objects and maybe even  sort them. &lt;br /&gt;
&lt;br /&gt;
In Ruby the comparable behavior is achieved by simply defining the &amp;lt;=&amp;gt;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. &lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 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 by writing our code in there to compare the two objects of that class. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about Organizations. Suppose we have to sort different organizations as to which is greater by comparing the total revenue of each. The implementation in Java is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
'''Enumerable module'''&lt;br /&gt;
&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
If  the  classes which have included the Enumerable module can implement the rocket method &amp;lt;=&amp;gt;,  we can also use other methods like min, max ,sort on the collections.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''Usage of Enumerable'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                    include Enumerable&lt;br /&gt;
                    #other code&lt;br /&gt;
                     def each&lt;br /&gt;
                      #definiton&lt;br /&gt;
                     end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
*collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
eg:&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' 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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
 include Enumerable&lt;br /&gt;
    def initialize(string)&lt;br /&gt;
       @string=string&lt;br /&gt;
    end&lt;br /&gt;
 &lt;br /&gt;
    def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 #The numbers in a string are concatinated and returned.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Data Mapper==&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
Following are the steps involved in using the DataMapper&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 The models can be created as below&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
the code&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''DataMapper  in Java'''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior==&lt;br /&gt;
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.”''&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Wisher&lt;br /&gt;
  def wish&lt;br /&gt;
   puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
    include Wisher&lt;br /&gt;
  def initialize(name,age)&lt;br /&gt;
    @name=name&lt;br /&gt;
    @age=age&lt;br /&gt;
  end&lt;br /&gt;
    attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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 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 &amp;lt;=&amp;gt; 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
  include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
    puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Also classes implementing interfaces do 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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
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 [http://en.wikipedia.org/wiki/Aspect-oriented_programming aspect-oriented programming]  can produce full fledged mixins in  Java&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50330</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50330"/>
		<updated>2011-09-22T19:22:12Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Here we discuss about the interfaces in Java, mixins in Ruby and compare some of the functionalities performed by Ruby and the corresponding behaviors if exhibited by the interfaces. &lt;br /&gt;
==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
'''Comparable''' is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable''' is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
'''FileTest''' is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
'''Kernel''' is a module included by the Object class; it defines Ruby’s ‘built-in’ methods.&lt;br /&gt;
 &lt;br /&gt;
'''Math''' is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
&lt;br /&gt;
'''ObjectSpace''' is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
'''Precision''' is a mixin for concrete numeric classes with precision&lt;br /&gt;
&lt;br /&gt;
==Comparable:==&lt;br /&gt;
Suppose there is a requirement where we have to compare few objects and sort them. 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. &lt;br /&gt;
Consider a situation where we have a class Organization and this Organization stores the information about Organizations. Suppose we have to sort different organizations as to which is greater by comparing the total revenue of each. The implementation in Java is as follows.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
    def to_s&lt;br /&gt;
    &amp;quot;#{name}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
org3=Org.new('org3',100002,4000);&lt;br /&gt;
org4=Org.new('org4',100003,4000);&lt;br /&gt;
org5=Org.new('org5',100004,4000);&lt;br /&gt;
a=[org1,org2,org3,org4,org5];&lt;br /&gt;
puts a.sort &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
org1&lt;br /&gt;
org2&lt;br /&gt;
org3&lt;br /&gt;
org4&lt;br /&gt;
org5&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Enumerable Behavior ==&lt;br /&gt;
Enumerable module&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&amp;lt;br /&amp;gt;&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
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.&lt;br /&gt;
'''Some Useful  Methods offered by Enumerable:'''&lt;br /&gt;
*map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
*grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
*any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
'''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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
'''Enumerable Interface in Java'''.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
'''hasMoreElements''': Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
'''nextElement''': the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior:==&lt;br /&gt;
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.”&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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. &lt;br /&gt;
  &lt;br /&gt;
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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50245</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50245"/>
		<updated>2011-09-22T04:38:58Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Pre-defined mixin modules ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&amp;lt;br /&amp;gt;&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
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.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
hasMoreElements: Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
nextElement: the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Singleton Behavior:==&lt;br /&gt;
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.”&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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. &lt;br /&gt;
  &lt;br /&gt;
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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==References:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50241</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50241"/>
		<updated>2011-09-22T04:36:02Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&amp;lt;br /&amp;gt;&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
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.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
hasMoreElements: Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
nextElement: the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
*’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
*Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
*	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
*	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&amp;lt;br /&amp;gt;&lt;br /&gt;
Step -1:  Provide a default private constructor.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&amp;lt;br /&amp;gt;&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning.&amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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. &lt;br /&gt;
  &lt;br /&gt;
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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50238</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50238"/>
		<updated>2011-09-22T04:32:51Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&amp;lt;br /&amp;gt;&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
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.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
hasMoreElements: Tests if this enumeration contains more elements.&amp;lt;br /&amp;gt;&lt;br /&gt;
nextElement: the next element of this enumeration.&amp;lt;br /&amp;gt;&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
*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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
*'''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.&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
*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. &lt;br /&gt;
  &lt;br /&gt;
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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*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.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50235</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50235"/>
		<updated>2011-09-22T04:26:26Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&amp;lt;br /&amp;gt;&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50233</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50233"/>
		<updated>2011-09-22T04:24:51Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;br /&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;br /&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;br /&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;br /&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;br /&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;br /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50231</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50231"/>
		<updated>2011-09-22T04:23:16Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;br /&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:''' &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;/br&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50229</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50229"/>
		<updated>2011-09-22T04:20:12Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
*	Instance of a module cannot be created.&lt;br /&gt;
*	It cannot be inherited by other classes but can be included.&lt;br /&gt;
*	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&amp;lt;br /&amp;gt;&lt;br /&gt;
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 (::)&amp;lt;be &amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;/br&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50225</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50225"/>
		<updated>2011-09-22T04:16:32Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;/br&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50224</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50224"/>
		<updated>2011-09-22T04:15:09Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;/br&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50222</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50222"/>
		<updated>2011-09-22T04:14:03Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Output:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 org2 is greater&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                   Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
                   #other code&lt;br /&gt;
                   def each&lt;br /&gt;
                   #definiton&lt;br /&gt;
                   end&lt;br /&gt;
                   #othercode&lt;br /&gt;
                   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
'''Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
 %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.  &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
%w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
''' output:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 123007 &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;$sudo gem install dm-core&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using&lt;br /&gt;
datamapper.setup&lt;br /&gt;
&lt;br /&gt;
 ''' '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
&amp;lt;pre&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &amp;lt;/br&amp;gt;&lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&amp;lt;/br&amp;gt;&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&amp;lt;/br&amp;gt;&lt;br /&gt;
http://ruby-doc.org/&amp;lt;/br&amp;gt;&lt;br /&gt;
http://download.oracle.com/javase/tutorial&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&amp;lt;/br&amp;gt;&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&amp;lt;/br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50214</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50214"/>
		<updated>2011-09-22T04:01:48Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
              interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50213</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50213"/>
		<updated>2011-09-22T03:57:57Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
 (((  interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } )))&lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
           (((  interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           })))&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 (((&lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
)))&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50212</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50212"/>
		<updated>2011-09-22T03:55:42Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
             interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''&lt;br /&gt;
 &lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50211</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50211"/>
		<updated>2011-09-22T03:54:54Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
             interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:''' :&lt;br /&gt;
 &lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50210</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50210"/>
		<updated>2011-09-22T03:52:19Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
             interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
'''Output:'''  &lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50209</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50209"/>
		<updated>2011-09-22T03:48:45Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
             interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
Output:  &amp;lt;br /&amp;gt;&lt;br /&gt;
Drawing Square &amp;lt;br /&amp;gt;&lt;br /&gt;
Area is 25.0 &amp;lt;br /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50208</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50208"/>
		<updated>2011-09-22T03:45:57Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
Contractual Obligation&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
             &lt;br /&gt;
             interface Shape&lt;br /&gt;
             {&lt;br /&gt;
                  void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
             }&lt;br /&gt;
             class Square implements Shape&lt;br /&gt;
             { &lt;br /&gt;
                  public void draw()&lt;br /&gt;
                  {&lt;br /&gt;
                	  System.out.println(&amp;quot;Drawing Square&amp;quot;);&lt;br /&gt;
                  }&lt;br /&gt;
                  public void displayArea(float a,float b) &lt;br /&gt;
                     { &lt;br /&gt;
                          System.out.println(&amp;quot;Area is &amp;quot;+a*b);}&lt;br /&gt;
                     }&lt;br /&gt;
             public class InfDemo{&lt;br /&gt;
                  public static void main(String args[])&lt;br /&gt;
                   {  Square s=new Square();&lt;br /&gt;
                      s.draw();&lt;br /&gt;
                      s.displayArea(5,5);&lt;br /&gt;
                   }&lt;br /&gt;
           }&lt;br /&gt;
Output: &lt;br /&gt;
Drawing Square&lt;br /&gt;
Area is 25.0&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50200</id>
		<title>CSC/ECE 517 Fall 2011/ch1 2c rs</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch1_2c_rs&amp;diff=50200"/>
		<updated>2011-09-22T03:34:45Z</updated>

		<summary type="html">&lt;p&gt;Vmallad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Interfaces in Java==&lt;br /&gt;
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   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.&lt;br /&gt;
   interface &amp;lt;interface name&amp;gt;&lt;br /&gt;
    { &amp;lt;constants&amp;gt;&lt;br /&gt;
          &amp;lt;method declarations&amp;gt;&lt;br /&gt;
      } &lt;br /&gt;
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.&lt;br /&gt;
===Contractual Obligation=== &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Interface:'''&lt;br /&gt;
&lt;br /&gt;
                {void draw();  &lt;br /&gt;
                  void displayArea(float a,float b);&lt;br /&gt;
                  }&lt;br /&gt;
    class Square implements Shape&lt;br /&gt;
           {  void draw() { System.out.println(“Drawing Square”);}&lt;br /&gt;
               void displayArea(float a,float b) &lt;br /&gt;
                     { System.out.println(“Area is ”+a*b);}&lt;br /&gt;
               }&lt;br /&gt;
Class InfDemo&lt;br /&gt;
 public static void main(String args[])&lt;br /&gt;
{  Square s=new Square();&lt;br /&gt;
     s.draw();&lt;br /&gt;
     s.displayArea(5,5);&lt;br /&gt;
}&lt;br /&gt;
Output: Drawing Square&lt;br /&gt;
             Area is 25.&lt;br /&gt;
&lt;br /&gt;
==Modules in Ruby==&lt;br /&gt;
A module in Ruby has the same implementation and is similar to class except a few significant differences:&lt;br /&gt;
1.	Instance of a module cannot be created.&lt;br /&gt;
2.	It cannot be inherited by other classes but can be included.&lt;br /&gt;
3.	The syntax to define module is different.&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Module:'''&lt;br /&gt;
module GoodGuy&lt;br /&gt;
def GoodGuy.bow&lt;br /&gt;
puts &amp;quot;I bow to you. you are a good guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
module BadGuy&lt;br /&gt;
def BadGuy.bow&lt;br /&gt;
puts &amp;quot;I will hit you with a bow.you are a bad guy&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixins in Ruby==&lt;br /&gt;
===History of Mixins:===&lt;br /&gt;
Mixins are not new. Smalltalk supported them back in 1971. &lt;br /&gt;
 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:[1] 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 &amp;quot;Mix-in&amp;quot;, his own trademarked term at the time . &lt;br /&gt;
===Mixins===&lt;br /&gt;
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.&lt;br /&gt;
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 (::)&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Example of Mixins:'''&lt;br /&gt;
class Actnow&lt;br /&gt;
require 'GoodGuy'&lt;br /&gt;
require 'BadGuy'&lt;br /&gt;
def Act(kind)&lt;br /&gt;
if(kind=='good')&lt;br /&gt;
GoodGuy.bow&lt;br /&gt;
end&lt;br /&gt;
if (kind =='bad')&lt;br /&gt;
BadGuy.bow&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
myobj=Actnow.new&lt;br /&gt;
myobj.Act('good')&lt;br /&gt;
myobj.Act('bad')&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==PRE-DEFINED MIXIN MODULES ==&lt;br /&gt;
Ruby has the  following built into modules: Comparable, Enumerable, FileTest, GC, Kernel, Math, ObjectSpace, Preci-sion, Process, Signal &lt;br /&gt;
&lt;br /&gt;
Comparable is a mixin module which permits the including class to implement comparison operators. The including class must define the &amp;lt;=&amp;gt; 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 &amp;lt;=&amp;gt; to implement the conventional comparison operators (&amp;lt;, &amp;lt;=, ==, &amp;gt;=, and &amp;gt;) and the method between?. &lt;br /&gt;
&lt;br /&gt;
Enumerable is a mix-in module for enumeration. The including class must provide the method each. &lt;br /&gt;
&lt;br /&gt;
FileTest is a module containing file test functions; its methods can also be accessed from the File class. &lt;br /&gt;
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. &lt;br /&gt;
Kernel is a module included by the Object class; it defines Ruby’s ‘built-in’ methods. &lt;br /&gt;
Math is a module containing module functions for basic trigonometric and transcendental functions. &lt;br /&gt;
ObjectSpace is a module which contains routines that interact with the garbage collection facility and allows traversing   all living objects with an iterator. &lt;br /&gt;
Precision is a mixin for concrete numeric classes with precision&lt;br /&gt;
==Comparable:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
'''Example of Comparable in Java:'''&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.Collections;&lt;br /&gt;
import java.util.Iterator;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
&lt;br /&gt;
//import java.util.*;&lt;br /&gt;
public class Org implements Comparable {&lt;br /&gt;
	String name;&lt;br /&gt;
	Integer revenue;&lt;br /&gt;
	int emplCount;&lt;br /&gt;
	public Org(String name,Integer revenue, int emplCount)&lt;br /&gt;
	{&lt;br /&gt;
		this.name=name;&lt;br /&gt;
		this.revenue=revenue;&lt;br /&gt;
		this.emplCount=emplCount;&lt;br /&gt;
		&lt;br /&gt;
	}&lt;br /&gt;
	public int compareTo(Object obj1)&lt;br /&gt;
	{&lt;br /&gt;
		if (this.revenue == ((Org) obj1).revenue)&lt;br /&gt;
	           return 0;&lt;br /&gt;
	       else if ((this.revenue) &amp;gt; ((Org) obj1).revenue)&lt;br /&gt;
	           return 1;&lt;br /&gt;
	       else&lt;br /&gt;
	           return -1;&lt;br /&gt;
	}&lt;br /&gt;
	 public String toString() {&lt;br /&gt;
	       return &amp;quot;Org &amp;quot; + name  + &amp;quot;\n&amp;quot; ;&lt;br /&gt;
	   }&lt;br /&gt;
	 public static void main(String[] args) {&lt;br /&gt;
	      List list1 = new ArrayList();&lt;br /&gt;
			Org org1=new Org(&amp;quot;org1&amp;quot;,1000000,5000);&lt;br /&gt;
			Org org2=new Org(&amp;quot;org2&amp;quot;,1000001,4000);&lt;br /&gt;
			Org org3=new Org(&amp;quot;org3&amp;quot;,1000002,4000);&lt;br /&gt;
			Org org4=new Org(&amp;quot;org4&amp;quot;,1000003,4000);&lt;br /&gt;
			Org org5=new Org(&amp;quot;org5&amp;quot;,1000004,4000);&lt;br /&gt;
			Org org6=new Org(&amp;quot;org6&amp;quot;,1000005,4000);&lt;br /&gt;
			&lt;br /&gt;
	list1.add(org1);&lt;br /&gt;
	list1.add(org2);&lt;br /&gt;
	list1.add(org3);&lt;br /&gt;
	list1.add(org4);&lt;br /&gt;
	list1.add(org5);&lt;br /&gt;
	list1.add(org6);&lt;br /&gt;
	      Collections.sort(list1);&lt;br /&gt;
	      Iterator itr = list1.iterator();&lt;br /&gt;
	      System.out.println(&amp;quot;The list of organizations in the ascending order of revenue are&amp;quot;);&lt;br /&gt;
	      while(itr.hasNext()){&lt;br /&gt;
	          Object element = itr.next();&lt;br /&gt;
	          System.out.println(element + &amp;quot;\n&amp;quot;);   &lt;br /&gt;
	      }&lt;br /&gt;
	}&lt;br /&gt;
	&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
The list of organizations in the ascending order of revenue are&lt;br /&gt;
Org org1&lt;br /&gt;
&lt;br /&gt;
Org org2&lt;br /&gt;
&lt;br /&gt;
Org org3&lt;br /&gt;
&lt;br /&gt;
Org org4&lt;br /&gt;
&lt;br /&gt;
Org org5&lt;br /&gt;
&lt;br /&gt;
Org org6&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
'''Example of Comparable in Ruby:'''&lt;br /&gt;
class Org&lt;br /&gt;
      include Comparable&lt;br /&gt;
    attr :name&lt;br /&gt;
    attr :revenue&lt;br /&gt;
    attr :emplCount&lt;br /&gt;
    def initialize(name,revenue,emplCount)&lt;br /&gt;
      @name = name&lt;br /&gt;
      @revenue=revenue&lt;br /&gt;
      @emplCount=emplCount&lt;br /&gt;
    end&lt;br /&gt;
    def &amp;lt;=&amp;gt;(second)&lt;br /&gt;
      self.revenue &amp;lt;=&amp;gt; second.revenue&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
org1=Org.new('org1',100000,5000);&lt;br /&gt;
org2=Org.new('org2',100001,4000);&lt;br /&gt;
if org1&amp;gt;org2&lt;br /&gt;
puts &amp;quot; org1 is greater&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
if org2&amp;gt;org1&lt;br /&gt;
puts &amp;quot; org2 is greater&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
output: org2 is greater&lt;br /&gt;
&lt;br /&gt;
==ENUMERABLE BEHAVIOR ==&lt;br /&gt;
ENUMERABLE MODULE&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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&lt;br /&gt;
Usage of Enumerables.&lt;br /&gt;
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.&lt;br /&gt;
 It gives objects of collections additional collection specific behaviors. These behaviors are given by the each method.&lt;br /&gt;
Following is the list of methods provided by the Enumerable mixin.&lt;br /&gt;
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  &lt;br /&gt;
&lt;br /&gt;
''' To Mix in Enumerable in a class,'''&lt;br /&gt;
&lt;br /&gt;
Class MyCollection&lt;br /&gt;
                   include Enumerable&lt;br /&gt;
   #other code&lt;br /&gt;
   def each&lt;br /&gt;
  #definiton&lt;br /&gt;
end&lt;br /&gt;
#othercode&lt;br /&gt;
end.&lt;br /&gt;
Some of the built in classes which mixin Enumerable are : String, Hash ,Array ,Range ,Struct.&lt;br /&gt;
Each class that includes ‘Enumerable’ must define the ‘each’ method as per its own requirement.&lt;br /&gt;
eg: the Array class ‘s each method yields each element.&lt;br /&gt;
        The Hash class‘s each yields each key-value pair as a two element array.&lt;br /&gt;
Some Useful  Methods offered by Enumerable:&lt;br /&gt;
map:  modifies each member according to the instructions in a block and returns the modified collection of members.&lt;br /&gt;
.collect:  similar to map.&lt;br /&gt;
grep: The grep method ‘searches’ for members using a regular expression &lt;br /&gt;
Eg: (1..10).grep  (5..7)   #=&amp;gt;[5,6,7]&lt;br /&gt;
find?&lt;br /&gt;
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.&lt;br /&gt;
Eg. %w{ant bear cat}.all? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.all? {|word| word.length &amp;gt;= 4}   #=&amp;gt; false&lt;br /&gt;
   &lt;br /&gt;
any? &lt;br /&gt;
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.   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 3}   #=&amp;gt; true&lt;br /&gt;
   %w{ant bear cat}.any? {|word| word.length &amp;gt;= 4}   #=&amp;gt; true&lt;br /&gt;
   &lt;br /&gt;
 &lt;br /&gt;
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&lt;br /&gt;
collection and then applies the operation to the result of this computation and to the third&lt;br /&gt;
element, and so on, until all elements in the collection have been used.&lt;br /&gt;
&lt;br /&gt;
''' Example of Enumerable'''&lt;br /&gt;
class EnumerableDemo&lt;br /&gt;
  include Enumerable&lt;br /&gt;
 def initialize(string)&lt;br /&gt;
  @string=string&lt;br /&gt;
end &lt;br /&gt;
def each &lt;br /&gt;
       @string.scan(/\d/) do |num|&lt;br /&gt;
           yield num    &lt;br /&gt;
        end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
ed=EnumerableDemo.new(&amp;quot;123Bond007&amp;quot;)&lt;br /&gt;
puts ed.inject(:+)   # output: 123007 &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
ENUMERABLE INTERFACE IN JAVA.&lt;br /&gt;
 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.&lt;br /&gt;
 Successive calls to the nextElement method return successive elements of the series. &lt;br /&gt;
''' For example, to print all elements of a vector v:'''&lt;br /&gt;
     for (Enumeration e = v.elements() ; e.hasMoreElements() ;) {&lt;br /&gt;
         System.out.println(e.nextElement());&lt;br /&gt;
&lt;br /&gt;
     }&lt;br /&gt;
 hasMoreElements: Tests if this enumeration contains more elements.&lt;br /&gt;
nextElement: the next element of this enumeration.&lt;br /&gt;
Note: The functionality of this interface is duplicated by the Iterator interface&lt;br /&gt;
==Data Mapper:==&lt;br /&gt;
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. &lt;br /&gt;
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.&lt;br /&gt;
Below are the steps involved in using the DataMapper&lt;br /&gt;
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.&lt;br /&gt;
''' '''&lt;br /&gt;
&lt;br /&gt;
$sudo gem install dm-core&lt;br /&gt;
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.&lt;br /&gt;
''''''&lt;br /&gt;
&lt;br /&gt;
Eg: For SQLite 3&lt;br /&gt;
$ sudo apt-get install libsqlite3-dev&lt;br /&gt;
$ sudo gem install dm-sqlite-adapter&lt;br /&gt;
&lt;br /&gt;
'''''&lt;br /&gt;
&lt;br /&gt;
Step 3: We then need to require the dm-core gem in our application.&lt;br /&gt;
Require ‘rubygems’&lt;br /&gt;
Require’data_mapper’&lt;br /&gt;
Require ‘dm-core’&lt;br /&gt;
Now, we have to specify the database connection to which our DataMapper must talk to. This is done using datamapper.setup&lt;br /&gt;
 ''' '''&lt;br /&gt;
&lt;br /&gt;
DataMapper.setup(:default, “[dataobject]://[relative-path]”)&lt;br /&gt;
For SQLite, this would be something like this.&lt;br /&gt;
DataMapper.setup( :default, &amp;quot;sqlite3://#databases/myown.db&amp;quot; )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Step-4 – Define models in a class using DataMapper:&lt;br /&gt;
''' The models can be created as below'''&lt;br /&gt;
# Create and define a class Recipee&lt;br /&gt;
  class Recipee&lt;br /&gt;
  include DataMapper::Resource&lt;br /&gt;
&lt;br /&gt;
  property :Name, String: key =&amp;gt; true&lt;br /&gt;
  property :type, String&lt;br /&gt;
  property :Description, text&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
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=&amp;gt; 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.&lt;br /&gt;
Now, we can access these as if they were objects without having knowledge about any query language to insert and update a table. &lt;br /&gt;
'''the code'''&lt;br /&gt;
# Create a new record&lt;br /&gt;
myrecipee = Recipee.new&lt;br /&gt;
myrecipee.attributes = {&lt;br /&gt;
  :name =&amp;gt; 'OrangeJuice',&lt;br /&gt;
  :type =&amp;gt; 'beverage',&lt;br /&gt;
  :Description =&amp;gt; 'Tasty!'&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Insert: myrecipee.save to save&lt;br /&gt;
&lt;br /&gt;
Modify: myrecipee.Description=’Tasty and Delicious!’&lt;br /&gt;
Delete: Myrecipee.destroy to deletethe tuple in the database&lt;br /&gt;
&lt;br /&gt;
Select: puts myrecipee.inspect&lt;br /&gt;
&lt;br /&gt;
DataMappers in JAVA:&lt;br /&gt;
 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.&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==SINGLETON BEHAVIOR:==&lt;br /&gt;
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.”&lt;br /&gt;
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.&lt;br /&gt;
In Ruby, the singleton behavior can be achieved by simply including the ‘Singleton’ Module.&lt;br /&gt;
To use ruby singleton module, &lt;br /&gt;
1.’require’ the ‘Singleton’ and then include it in desired class.&lt;br /&gt;
2.Use the instance method to get the instance you need.&lt;br /&gt;
Ruby does the following when a singleton module is included in a class.&lt;br /&gt;
1.	New method is made private so that it can’t be used for instantiation.&lt;br /&gt;
2.	A class method called ‘instance’ is added that instantiates only one instance of the class.&lt;br /&gt;
&lt;br /&gt;
''' Eg: Usage:'''&lt;br /&gt;
   class A&lt;br /&gt;
      include Singleton&lt;br /&gt;
      # ...&lt;br /&gt;
   end&lt;br /&gt;
this ensures that only one instance of A  be created.&lt;br /&gt;
  a,b  = A.instance, A.instance&lt;br /&gt;
  a == b    # =&amp;gt; true&lt;br /&gt;
  A.new               #  NoMethodError - new is private ...&lt;br /&gt;
 Instance  is created at the first call of A.instance(), thus&lt;br /&gt;
  &lt;br /&gt;
This behavior is preserved under inheritance and cloning.This is achieved by marking A.new  as private&lt;br /&gt;
And providing (or modifying) the class methods A.inherited() and A.clone() - to ensure that the Singletonpattern is properly inherited and cloned.&lt;br /&gt;
&lt;br /&gt;
In java, the singleton design pattern proposes that at any time there can be a single instance of an object created by JVM.&lt;br /&gt;
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.&lt;br /&gt;
For implementing a singleton pattern, consider the following steps:&lt;br /&gt;
Step -1:  Provide a default private constructor.&lt;br /&gt;
Step-2: Define a method to obtain the reference to the singleton Object.&lt;br /&gt;
Step 3: The Access method has to be made synchronized  to prevent Thread Problems.&lt;br /&gt;
Step 4: The Object clone method has to be overridden to prevent cloning&lt;br /&gt;
&lt;br /&gt;
''' Example of Singleton:'''&lt;br /&gt;
class SingletonClass {&lt;br /&gt;
&lt;br /&gt;
	private static SingletonClass singletonObject;&lt;br /&gt;
	/** A private Constructor prevents any other class from instantiating. */&lt;br /&gt;
	private SingletonClass() {&lt;br /&gt;
		//	 Optional Code&lt;br /&gt;
	}&lt;br /&gt;
	public static synchronized SingletonClass getSingletonObject() {&lt;br /&gt;
		if (singletonObject == null) {&lt;br /&gt;
			singletonObject = new SingletonClass();&lt;br /&gt;
		}&lt;br /&gt;
		return singletonObject;&lt;br /&gt;
	}&lt;br /&gt;
	public Object clone() throws CloneNotSupportedException {&lt;br /&gt;
		throw new CloneNotSupportedException();&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SingletonObjectDemo {&lt;br /&gt;
&lt;br /&gt;
	public static void main(String args[]) {&lt;br /&gt;
		//		SingletonClass obj = new SingletonClass();&lt;br /&gt;
                //Compilation error not allowed&lt;br /&gt;
		SingletonClass obj = SingletonClass.getSingletonObject();&lt;br /&gt;
		// Your Business Logic&lt;br /&gt;
		System.out.println(&amp;quot;Singleton object obtained&amp;quot;);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Mixin vs Interfaces==&lt;br /&gt;
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. &lt;br /&gt;
''' Example:'''&lt;br /&gt;
 module Wisher&lt;br /&gt;
 def wish&lt;br /&gt;
  puts &amp;quot;Good Day &amp;quot;+self.name  #name ie attr_reader method of the host class is used even before the class is define.&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 class Person&lt;br /&gt;
 include Wisher&lt;br /&gt;
 def initialize(name,age)&lt;br /&gt;
 @name=name&lt;br /&gt;
 @age=age&lt;br /&gt;
 end&lt;br /&gt;
 attr_reader :name&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
p=Person.new (“James”,4)&lt;br /&gt;
p.wish   #returns Good Day James.&lt;br /&gt;
Such a feature is not supported by interfaces in java.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
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.&lt;br /&gt;
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.   &lt;br /&gt;
 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 &amp;gt;,&amp;lt;,between? Etc then an runtime error occurs. &lt;br /&gt;
Also &lt;br /&gt;
class A&lt;br /&gt;
 include Comparable&lt;br /&gt;
 def hi&lt;br /&gt;
 puts &amp;quot;hi&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 a=A.new&lt;br /&gt;
&lt;br /&gt;
 a.hi  #returns  “hi”.&lt;br /&gt;
Implementing interfaces does not inherit code; it is purely something to help more programs&lt;br /&gt;
type-check in a statically typed language whereas mixins provide actual code to classes that include them. Hence mixins allow code reusability.&lt;br /&gt;
&lt;br /&gt;
==Conclusion:==&lt;br /&gt;
 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.&lt;br /&gt;
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&lt;br /&gt;
&lt;br /&gt;
==REFERENCES:==&lt;br /&gt;
http://ruby.about.com/od/sinatra/a/datamapper.htm . &lt;br /&gt;
http://solitude.vkps.co.uk/Archives/2009/01/13/data-mappers/&lt;br /&gt;
http://javatemple.blogspot.com/2008/11/features-of-hibernate-in-nutshell.html&lt;br /&gt;
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html&lt;br /&gt;
http://www.tutorialspoint.com/ruby/ruby_modules.htm&lt;br /&gt;
http://www.javabeginner.com/learn-java/java-singleton-design-pattern&lt;br /&gt;
http://www.brainbell.com/tutorials/java/Applying_Some_Structure.htm&lt;br /&gt;
http://ruby-doc.org/&lt;br /&gt;
http://download.oracle.com/javase/tutorial&lt;br /&gt;
http://en.wikipedia.org/wiki/Mixin&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;/div&gt;</summary>
		<author><name>Vmallad</name></author>
	</entry>
</feed>