<?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=Vsubram3</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=Vsubram3"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Vsubram3"/>
	<updated>2026-05-26T06:58:04Z</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_2010/ch7_7a_AV&amp;diff=43474</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43474"/>
		<updated>2010-12-13T01:26:34Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Observer Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ 5] [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter 2] &lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. [http://sourcemaking.com/design_patterns 1]  &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem. [http://c2.com/cgi/wiki?DesignPatterns 3] &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible. [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table 11] [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_az chapter1]&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here. [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf 4]&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://c2.com/cgi/wiki?ObserverPattern 6]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig5_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 5.1 Use Case diagram for Observer pattern &lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm 8]&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit. [http://www.oodesign.com/decorator-pattern.html 7]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig6_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 6.1 Use Case diagram for Observer pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns. [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html 9]&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions. [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ 10]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig7_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 7.1 Use Case diagram for Iterator pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[1]: [http://sourcemaking.com/design_patterns Design Patterns, Source Making]&lt;br /&gt;
&lt;br /&gt;
*[2]: [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter Design pattern, computer_science wikipedia]&lt;br /&gt;
&lt;br /&gt;
*[3]: [http://c2.com/cgi/wiki?DesignPatterns Design Patterns wiki]&lt;br /&gt;
&lt;br /&gt;
*[4]: [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf irisa]&lt;br /&gt;
&lt;br /&gt;
*[5]: [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ Slides, uottawa]&lt;br /&gt;
&lt;br /&gt;
*[6]: [http://c2.com/cgi/wiki?ObserverPattern Observer Pattern, c2.com]&lt;br /&gt;
&lt;br /&gt;
*[7]: [http://www.oodesign.com/decorator-pattern.html Decorator Pattern, oodesign]&lt;br /&gt;
&lt;br /&gt;
*[8]: [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm Ruby Design Patterns, Observer Pattern, oreillynet]&lt;br /&gt;
&lt;br /&gt;
*[9]: [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Decorator Pattern with Ruby, lukeredpath]&lt;br /&gt;
&lt;br /&gt;
*[10]: [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Iterators in Ruby, devarticles]&lt;br /&gt;
&lt;br /&gt;
*[11]: [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table uottawa]&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43473</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43473"/>
		<updated>2010-12-13T01:25:49Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Implementation of Decorator pattern in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ 5] [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter 2] &lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. [http://sourcemaking.com/design_patterns 1]  &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem. [http://c2.com/cgi/wiki?DesignPatterns 3] &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible. [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table 11] [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch1_1e_az chapter1]&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here. [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf 4]&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://c2.com/cgi/wiki?ObserverPattern 6]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig5_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 5.1 Use Case diagram for Observer pattern &lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm 8]&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit. [http://www.oodesign.com/decorator-pattern.html 7]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig6_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 6.1 Use Case diagram for Observer pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns. [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html 9]&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions. [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ 10]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig7_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 7.1 Use Case diagram for Iterator pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[1]: [http://sourcemaking.com/design_patterns Design Patterns, Source Making]&lt;br /&gt;
&lt;br /&gt;
*[2]: [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter Design pattern, computer_science wikipedia]&lt;br /&gt;
&lt;br /&gt;
*[3]: [http://c2.com/cgi/wiki?DesignPatterns Design Patterns wiki]&lt;br /&gt;
&lt;br /&gt;
*[4]: [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf irisa]&lt;br /&gt;
&lt;br /&gt;
*[5]: [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ Slides, uottawa]&lt;br /&gt;
&lt;br /&gt;
*[6]: [http://c2.com/cgi/wiki?ObserverPattern Observer Pattern, c2.com]&lt;br /&gt;
&lt;br /&gt;
*[7]: [http://www.oodesign.com/decorator-pattern.html Decorator Pattern, oodesign]&lt;br /&gt;
&lt;br /&gt;
*[8]: [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm Ruby Design Patterns, Observer Pattern, oreillynet]&lt;br /&gt;
&lt;br /&gt;
*[9]: [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Decorator Pattern with Ruby, lukeredpath]&lt;br /&gt;
&lt;br /&gt;
*[10]: [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Iterators in Ruby, devarticles]&lt;br /&gt;
&lt;br /&gt;
*[11]: [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table uottawa]&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43014</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43014"/>
		<updated>2010-12-01T15:40:50Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Example square_arrayElements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ 5] [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter 2]&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. [http://sourcemaking.com/design_patterns 1]  &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem. [http://c2.com/cgi/wiki?DesignPatterns 3] &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible. [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table 11]&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here. [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf 4]&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://c2.com/cgi/wiki?ObserverPattern 6]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig5_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 5.1 Use Case diagram for Observer pattern &lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm 8]&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit. [http://www.oodesign.com/decorator-pattern.html 7]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig6_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 6.1 Use Case diagram for Observer pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns. [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html 9]&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions. [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ 10]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig7_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 7.1 Use Case diagram for Iterator pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[1]: [http://sourcemaking.com/design_patterns Design Patterns (Source Making)]&lt;br /&gt;
&lt;br /&gt;
*[2]: [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter Design pattern (computer_science) wikipedia.org]&lt;br /&gt;
&lt;br /&gt;
*[3]: [http://c2.com/cgi/wiki?DesignPatterns Design Patterns wiki]&lt;br /&gt;
&lt;br /&gt;
*[4]: [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf irisa.fr]&lt;br /&gt;
&lt;br /&gt;
*[5]: [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ Slides, uottawa.ca]&lt;br /&gt;
&lt;br /&gt;
*[6]: [http://c2.com/cgi/wiki?ObserverPattern Observer Pattern, c2.com]&lt;br /&gt;
&lt;br /&gt;
*[7]: [http://www.oodesign.com/decorator-pattern.html Decorator Pattern, oodesign.com]&lt;br /&gt;
&lt;br /&gt;
*[8]: [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm Ruby Design Patterns, Observer Pattern, oreillynet.com]&lt;br /&gt;
&lt;br /&gt;
*[9]: [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Decorator Pattern with Ruby, lukeredpath.co.uk]&lt;br /&gt;
&lt;br /&gt;
*[10]: [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Iterators in Ruby, devarticles.com]&lt;br /&gt;
&lt;br /&gt;
*[11]: [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table uottawa.ca]&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43013</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43013"/>
		<updated>2010-12-01T15:40:26Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Example  - square_arrayElements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ 5] [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter 2]&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. [http://sourcemaking.com/design_patterns 1]  &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem. [http://c2.com/cgi/wiki?DesignPatterns 3] &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible. [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table 11]&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here. [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf 4]&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://c2.com/cgi/wiki?ObserverPattern 6]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig5_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 5.1 Use Case diagram for Observer pattern &lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm 8]&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit. [http://www.oodesign.com/decorator-pattern.html 7]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig6_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 6.1 Use Case diagram for Observer pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns. [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html 9]&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions. [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ 10]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig7_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 7.1 Use Case diagram for Iterator pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[1]: [http://sourcemaking.com/design_patterns Design Patterns (Source Making)]&lt;br /&gt;
&lt;br /&gt;
*[2]: [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter Design pattern (computer_science) wikipedia.org]&lt;br /&gt;
&lt;br /&gt;
*[3]: [http://c2.com/cgi/wiki?DesignPatterns Design Patterns wiki]&lt;br /&gt;
&lt;br /&gt;
*[4]: [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf irisa.fr]&lt;br /&gt;
&lt;br /&gt;
*[5]: [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ Slides, uottawa.ca]&lt;br /&gt;
&lt;br /&gt;
*[6]: [http://c2.com/cgi/wiki?ObserverPattern Observer Pattern, c2.com]&lt;br /&gt;
&lt;br /&gt;
*[7]: [http://www.oodesign.com/decorator-pattern.html Decorator Pattern, oodesign.com]&lt;br /&gt;
&lt;br /&gt;
*[8]: [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm Ruby Design Patterns, Observer Pattern, oreillynet.com]&lt;br /&gt;
&lt;br /&gt;
*[9]: [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Decorator Pattern with Ruby, lukeredpath.co.uk]&lt;br /&gt;
&lt;br /&gt;
*[10]: [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Iterators in Ruby, devarticles.com]&lt;br /&gt;
&lt;br /&gt;
*[11]: [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table uottawa.ca]&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43012</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43012"/>
		<updated>2010-12-01T15:40:09Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Implementation of Iterator pattern in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ 5] [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter 2]&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. [http://sourcemaking.com/design_patterns 1]  &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem. [http://c2.com/cgi/wiki?DesignPatterns 3] &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible. [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table 11]&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here. [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf 4]&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://c2.com/cgi/wiki?ObserverPattern 6]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig5_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 5.1 Use Case diagram for Observer pattern &lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place. [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm 8]&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit. [http://www.oodesign.com/decorator-pattern.html 7]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig6_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 6.1 Use Case diagram for Observer pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns. [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html 9]&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions. [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ 10]&lt;br /&gt;
&lt;br /&gt;
[[Image:Fig7_1.png]]&lt;br /&gt;
&lt;br /&gt;
Fig 7.1 Use Case diagram for Iterator pattern&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example  - square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
*[1]: [http://sourcemaking.com/design_patterns Design Patterns (Source Making)]&lt;br /&gt;
&lt;br /&gt;
*[2]: [http://en.wikipedia.org/wiki/Design_pattern_(computer_science)?utm_source=twitterfeed&amp;amp;utm_medium=twitter Design pattern (computer_science) wikipedia.org]&lt;br /&gt;
&lt;br /&gt;
*[3]: [http://c2.com/cgi/wiki?DesignPatterns Design Patterns wiki]&lt;br /&gt;
&lt;br /&gt;
*[4]: [http://www.irisa.fr/prive/jezequel/enseignement/dp.pdf irisa.fr]&lt;br /&gt;
&lt;br /&gt;
*[5]: [http://www.site.uottawa.ca/school/research/lloseng/supportMaterial/slides/ Slides, uottawa.ca]&lt;br /&gt;
&lt;br /&gt;
*[6]: [http://c2.com/cgi/wiki?ObserverPattern Observer Pattern, c2.com]&lt;br /&gt;
&lt;br /&gt;
*[7]: [http://www.oodesign.com/decorator-pattern.html Decorator Pattern, oodesign.com]&lt;br /&gt;
&lt;br /&gt;
*[8]: [http://www.oreillynet.com/ruby/.../ruby_design_patterns_observer.htm Ruby Design Patterns, Observer Pattern, oreillynet.com]&lt;br /&gt;
&lt;br /&gt;
*[9]: [http://lukeredpath.co.uk/blog/decorator-pattern-with-ruby-in-8-lines.html Decorator Pattern with Ruby, lukeredpath.co.uk]&lt;br /&gt;
&lt;br /&gt;
*[10]: [http://www.devarticles.com/c/a/Ruby-on-Rails/Iterators-in-Ruby/ Iterators in Ruby, devarticles.com]&lt;br /&gt;
&lt;br /&gt;
*[11]: [http://www.site.uottawa.ca:4321/oose/index.html#designpattern_table uottawa.ca]&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43000</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=43000"/>
		<updated>2010-12-01T07:59:43Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 - square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above.In the observer pattern the subject notifies any updates in the state to all its dependants (observers).The decorator pattern adds extra features to the already existing class.The iterator helps to access the elements of a collection object individually. The real-world situations where each design pattern can be used were illustrated with respective examples.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42999</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42999"/>
		<updated>2010-12-01T07:48:05Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Example 2 EvenOrOdd */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 - square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=conclusion=&lt;br /&gt;
Design patterns help novice software developers to write efficient programs. The novel implementations of a few designs patterns like observer, decorator and iterator in dynamic language like Ruby was discussed above. The real-world situations where each design pattern can be used were illustrated with examples.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42998</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42998"/>
		<updated>2010-12-01T07:44:23Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Example 1 - square_arrayElements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 - square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
The array elements 1,2,3,4 is passed to the function square_arrayElements. The iterator parses each element and sends it to the block where the square of each element is calculated and prints it.&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42997</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42997"/>
		<updated>2010-12-01T07:41:02Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;br /&gt;
&lt;br /&gt;
=Iterator pattern=&lt;br /&gt;
Iterator design pattern is used to traverse each element of a collection object sequentially. While using the iterator, the programmer need not worry about the underlying interface of the collection object.The iterator interface usually consists of  hasNext( ),next( ), isDone( ) functions.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Iterator pattern in ruby==&lt;br /&gt;
&lt;br /&gt;
===Example 1 - square_arrayElements===&lt;br /&gt;
&lt;br /&gt;
This is a simple example where in an iterator each is used to print the square of each element in an array using closures.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def square_arrayElements (array)&lt;br /&gt;
    array.each {|a| puts a*a } &lt;br /&gt;
        end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;  &lt;br /&gt;
array = [1,2,3,5]&lt;br /&gt;
square_arrayElements(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Output:&amp;lt;br&amp;gt;&lt;br /&gt;
1&amp;lt;br&amp;gt;&lt;br /&gt;
4&amp;lt;br&amp;gt;&lt;br /&gt;
9&amp;lt;br&amp;gt;&lt;br /&gt;
25&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Example 2 EvenOrOdd ===&lt;br /&gt;
In below program, the iterator traverses through each element of the array and checks if the element is odd or even. This check is performed in the block which prints whether the given element is odd or even.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def evenOrodd(array1)&lt;br /&gt;
array1.each { |i| &lt;br /&gt;
 if i % 2 == 0&lt;br /&gt;
       puts &amp;quot;#{i} is a even number&amp;quot;&lt;br /&gt;
     else&lt;br /&gt;
       puts &amp;quot;#{i} is a odd number&amp;quot;&lt;br /&gt;
     end&lt;br /&gt;
      &lt;br /&gt;
   }&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt; &lt;br /&gt;
array1 = [1,2,3,5]&lt;br /&gt;
evenOrodd(array)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Output&amp;lt;br&amp;gt;&lt;br /&gt;
1 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
2 is a even number&amp;lt;br&amp;gt;&lt;br /&gt;
3 is a odd number&amp;lt;br&amp;gt;&lt;br /&gt;
5 is a odd number&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42996</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42996"/>
		<updated>2010-12-01T07:25:16Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42995</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42995"/>
		<updated>2010-12-01T07:24:56Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* output */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns.&lt;br /&gt;
&lt;br /&gt;
Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42994</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42994"/>
		<updated>2010-12-01T07:24:28Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Implementation of Decorator pattern in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==output==&lt;br /&gt;
price of pizza without Topping 5&lt;br /&gt;
price of pizza with OnionTopping 6.1&lt;br /&gt;
price of pizza with cheese and Onion toppings 7.1&lt;br /&gt;
&lt;br /&gt;
In the example above, we have a class called SimplePizza which initializes the cost of a pizza without any toppings. There might be a situation where a customer would want a pizza with a topping instead writing the price for each type of pizza. For this porpose we use decorator patterns .&lt;br /&gt;
 Here the SimplePizza is the base class. The OnionToppingPizza and cheeseToppingPizza classes are the decorators used for this base class. This program dynamically calculates the price of each pizza depending on the choice of topping.&lt;br /&gt;
Advantage:&lt;br /&gt;
Altough we dint write a class to calculate the price of a TwoToppingPizza, The decorator pattern used in this program was able to calculate the price of TwotoppingPizzza accurately.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42993</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42993"/>
		<updated>2010-12-01T07:21:22Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Output: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This design pattern is used when additional functionalities have to be given to a particular class in a dynamic manner. It is an alternative for subclassing and inheritance (which is done statically). The most common application of decorator patterns is in designing of GUI toolkit.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Decorator pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;	&lt;br /&gt;
module Decorator&lt;br /&gt;
 def initialize(decorated)&lt;br /&gt;
    @decorated = decorated&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class SimplePizza&lt;br /&gt;
  def price&lt;br /&gt;
    @price = 5&lt;br /&gt;
     end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class OnionTopping&lt;br /&gt;
 include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @price = @decorated.price+ 1.1&lt;br /&gt;
   end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CheeseTopping&lt;br /&gt;
  include Decorator&lt;br /&gt;
&lt;br /&gt;
  def price&lt;br /&gt;
    @decorated.price + 1.0&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#Price of Pizza without decoration(no toppings)&lt;br /&gt;
 puts &amp;quot;price of pizza without Topping #{SimplePizza.new.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#Price of Pizza with OnionToppings (decorater used here)&lt;br /&gt;
#initialise OnionTopping class as a decorator for the class SimplePizza&lt;br /&gt;
OTPizza =OnionTopping.new(SimplePizza.new)&lt;br /&gt;
&lt;br /&gt;
puts &amp;quot;price of pizza with OnionTopping #{OTPizza.price}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
TwoTopping = CheeseTopping.new(OnionTopping.new(SimplePizza.new))&lt;br /&gt;
puts &amp;quot;price of pizza with cheese and Onion toppings #{TwoTopping.price}&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42992</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42992"/>
		<updated>2010-12-01T07:13:29Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Implementation of observer pattern in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Output:==&lt;br /&gt;
&lt;br /&gt;
The new price of the product is 12.5&lt;br /&gt;
The new price of the product is 12.0&lt;br /&gt;
The new price of the product is 14.5&lt;br /&gt;
&lt;br /&gt;
In the above example, we have the observable module that checks if a particular state is being updated or not. When the update happens the notify function is immediately called. &lt;br /&gt;
We maintain a seller class that uses the observable module, when the seller increases or decreases the price of a product the notify_observers is called which then calls the &lt;br /&gt;
modify_price which prints the new price. &lt;br /&gt;
This program uses observer pattern in a novel way using closures which differs from its implementation in any other static language.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42991</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42991"/>
		<updated>2010-12-01T07:11:48Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Implementation of observer pattern in ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module Observable #subject module which checks for update and notifies&lt;br /&gt;
def ModifyPrice( &amp;amp;callObservers )&lt;br /&gt;
@observers = [] &lt;br /&gt;
@observers &amp;lt;&amp;lt; callObservers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def notify_observers&lt;br /&gt;
@observers.each { |o| o.call } &lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Seller             #state that is checked frequently is price.&lt;br /&gt;
include Observable&lt;br /&gt;
attr :price&lt;br /&gt;
def initialize( price = 10 )&lt;br /&gt;
@price = price&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def increasePrice       &lt;br /&gt;
@price = @price +  2.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
def decreasePrice&lt;br /&gt;
@price = @price -  0.5&lt;br /&gt;
notify_observers&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
StoreKeeper = Seller.new&lt;br /&gt;
StoreKeeper.ModifyPrice do        #updates price and then notifies &lt;br /&gt;
puts &amp;quot;The new price of the product is #{StoreKeeper.price}&amp;quot;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
StoreKeeper.decreasePrice&lt;br /&gt;
StoreKeeper.increasePrice&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42990</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42990"/>
		<updated>2010-12-01T07:04:31Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Observer Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;br /&gt;
&lt;br /&gt;
==Implementation of observer pattern in ruby==&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42989</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42989"/>
		<updated>2010-12-01T06:55:22Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Design patterns in dynamic programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;br /&gt;
&lt;br /&gt;
=Observer Pattern=     &lt;br /&gt;
This pattern is used in the context, where frequent updates of different states take place.&lt;br /&gt;
It becomes an overhead if each object has to check whether the state has been updated or not. Hence all such objects (called the “observers”) register themselves to another object (“subject”). The subject is responsible to keep check on the object, and notify the observers when a change of state takes place.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42988</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42988"/>
		<updated>2010-12-01T06:53:57Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Importance of design patterns in programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42987</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42987"/>
		<updated>2010-12-01T06:53:15Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Importance of design patterns in programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42986</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42986"/>
		<updated>2010-12-01T06:52:56Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42985</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42985"/>
		<updated>2010-12-01T06:51:37Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* How to choose a particular design pattern? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Design patterns in dynamic programming languages=&lt;br /&gt;
&lt;br /&gt;
As dynamic languages need less overhead for bookkeeping (classes, methods) therefore design patterns are easier to use in dynamic languages. Dynamic o-o languages do not have class-restricted design, Hence design patterns are simpler and flexible.&lt;br /&gt;
&lt;br /&gt;
Some design patterns have novel implementation in dynamic languages. The alternative ways of implementing design patterns ( such as observer, decorator, iterator) is being discussed here.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42984</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42984"/>
		<updated>2010-12-01T06:48:56Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Design Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
==Design Pattern==&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages.&lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42983</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42983"/>
		<updated>2010-12-01T06:48:10Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* How to choose a particular design pattern? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
===Design Pattern===&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. &lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&amp;lt;br&amp;gt;&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42982</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42982"/>
		<updated>2010-12-01T06:47:43Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Importance of design patterns in programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
===Design Pattern===&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. &lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42981</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42981"/>
		<updated>2010-12-01T06:46:48Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Importance of design patterns in programming languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
===Design Pattern===&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. &lt;br /&gt;
&lt;br /&gt;
==Importance of design patterns in programming languages==&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==How to choose a particular design pattern?==&lt;br /&gt;
&lt;br /&gt;
i.	Identify the different classes and methods for a given problem.&lt;br /&gt;
ii.	Recognize the different solutions that can be used to solve the problem.&lt;br /&gt;
iii.	Compare and contrast the trade offs between the design patterns that can be used for that particular context.&lt;br /&gt;
iv.	Recognize the language limitations for each of the design patterns.&lt;br /&gt;
v.	Finally choose the most suitable and efficient design.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42980</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42980"/>
		<updated>2010-12-01T06:43:35Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
===Design Pattern===&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages. &lt;br /&gt;
&lt;br /&gt;
===Importance of design patterns in programming languages===&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem.&amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization. &amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42963</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42963"/>
		<updated>2010-12-01T05:51:09Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Novel implementations of design patterns in dynamic languages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42961</id>
		<title>CSC/ECE 517 Fall 2010/ch7 7a AV</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_7a_AV&amp;diff=42961"/>
		<updated>2010-12-01T05:44:24Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Novel implementations of design patterns in dynamic languages=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Design Pattern===&lt;br /&gt;
&lt;br /&gt;
It is a template that provides a description of good design alternatives for the benefit of programmers. It is used to provide recurring solutions to design patterns in both static and dynamic object oriented languages&lt;br /&gt;
&lt;br /&gt;
===Importance of design patterns in programming languages===&lt;br /&gt;
&lt;br /&gt;
i.	In a given context, design patterns can be a reusable solution for a general problem. &amp;lt;br&amp;gt;&lt;br /&gt;
ii.	For the benefit of novice programmers, design patterns are usually documented systematically. &amp;lt;br&amp;gt;&lt;br /&gt;
iii.	Design patterns help us to learn from the experience of other software developers. &amp;lt;br&amp;gt;&lt;br /&gt;
iv.	Design patterns are a framework for evolution and improvement of existing patterns. &amp;lt;br&amp;gt;&lt;br /&gt;
v.	It provides better abstraction for program organization.&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010_ch5_5f_aj&amp;diff=39774</id>
		<title>CSC/ECE 517 Fall 2010 ch5 5f aj</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010_ch5_5f_aj&amp;diff=39774"/>
		<updated>2010-11-02T16:05:55Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37967</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37967"/>
		<updated>2010-10-14T03:40:53Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Mixin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins = &lt;br /&gt;
&lt;br /&gt;
The concept of '''Inheritance''' and its working is explained in most of the [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) overviews. But a very few of these OOP overviews discuss alternatives when Inheritance fails to be the best design choice for an OO application. The main focus of this topic is to introduce the case where Inheritance proves to be a bad design option and discuss the concepts which are practiced to overcome it. The discussion starts with brief description of what Inheritance is and how it works. The classic scenario of the '''deadly Diamond problem''' is mentioned to discuss why exactly inheritance is criticized. The potential alternatives to solve this problem are introduced. We look at the merits and demerits of these alternatives. This topic discusses [http://en.wikipedia.org/wiki/Mixin/ Mixins] more closely. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in OOP Design ===&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of OOP that facilitates reuse and compartmentalization of code by creating objects (which are collections of attributes and behaviors) that can be derived from existing objects. The deriving object (class) is termed as the ''child class/sub-class/derived class'', and, the object (class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single inheritance creates a [http://en.wikipedia.org/wiki/Is-a/ Is-A] relationship amongst the classes. Consider the concept of single inheritance using class extension feature:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  class Animal{&lt;br /&gt;
      //...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Reptiles extends Animal{ &lt;br /&gt;
      //... &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the Reptiles class is a '''specialization''' of Animals class and derives some/all of its attributes and/or methods from the Animal class.&lt;br /&gt;
&lt;br /&gt;
The next section briefly discusses the alternatives of Inheritance used in various OO languages.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Definitions=&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++, Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent (and not so independent) concepts (classes) into a composite concept called the derived class. For instance, given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following figure depicts the use of multiple inheritance: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:UntitledMI.jpg|frame|upright|center|Figure 1. Multiple Inheritance]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A novel has a certain number of chapters, characters and other properties that can be thought as features of class STORY. It also has features of class BOOK like publisher, a price, a weight and an ISBN number. Thus, Class NOVEL should inherit from both.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is the code snippet showing the above mentioned example: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class STORY{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class BOOK{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class NOVEL : public STORY, public BOOK{         &lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple, object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''')  are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's the example showing how interfaces are used in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     interface Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly();&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
     public class Eagle implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“Eagle flies”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Eagle();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
     public class Duck implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“I cannot fly!!”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Duck();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the interface Bird has a method fly(). The Eagle class and the Duck class implement this method in different ways. Since each class making use of the interface has to implement all the methods of the interface, the methods in the interface can be implemented in different ways by different classes.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specified to be added to a particular class or object. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is an example code snippet showing the use of Modules in Ruby: [7]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   module takes_courses (parameter)&lt;br /&gt;
        def takes&lt;br /&gt;
             puts &amp;quot;student #{parameter} takes course csc517&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    class students&lt;br /&gt;
       include takes_courses                                                       -----&amp;gt; makes the module methods accessible&lt;br /&gt;
            def student_name( param )&lt;br /&gt;
            puts &amp;quot;student_name = #{param}&amp;quot;&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
    student = students.new&lt;br /&gt;
    #In class&lt;br /&gt;
    student.student_name 'john'                  #student name = John&lt;br /&gt;
    &lt;br /&gt;
    # In Module&lt;br /&gt;
    student.takes                                #student John takes course csc517&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example,A module called takes_courses is defined. This module contains a method called takes which  prints the name of the student who has taken the course csc517. The class called students includes the module takes_courses. A new student object is created. This object calls a method (student_name) defined in the class. The output would be &amp;quot;student_name = John &amp;quot;. The same student object is used to call the method takes in the module. This outputs &amp;quot;student John takes course csc 517&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that are closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes. It does not depend on the language syntax. It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
&lt;br /&gt;
Example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
Here, Each element in the array is printed.&lt;br /&gt;
&lt;br /&gt;
Following is an example showing the use of mixins: [8]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
module Draw&lt;br /&gt;
    def  draw_shape(param)&lt;br /&gt;
       puts &amp;quot;draw #{param}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class ParentClass&lt;br /&gt;
   def shape&lt;br /&gt;
       puts &amp;quot;shape is circle&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class Circle &amp;lt; ParentClass&lt;br /&gt;
      include Draw&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  f = Circle.new&lt;br /&gt;
  f.shape&lt;br /&gt;
  f.draw_shape &amp;quot;circle&amp;quot;&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example, a subclass circle inherits the methods defined in the parent class.The class circle also includes the drawModule. It can access the methods in the module.A new circle object is created which can access the methods in parent_class as well as the methods defined in the module. The output would be i)shape is a circle ii) draw circle&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Trait is not an abstract type and implements methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traits can be considered as a combination of interfaces that are abstract and their implementation. Here's a Scala trait representing objects that can be compared and ordered:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trait Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean&lt;br /&gt;
  def &amp;lt;=(that: Any): Boolean = (this &amp;lt; that) || (this == that)&lt;br /&gt;
  def &amp;gt; (that: Any): Boolean = !(this &amp;lt;= that)&lt;br /&gt;
  def &amp;gt;=(that: Any): Boolean = !(this &amp;lt; that)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Date extends Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean = /* implementation */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// can now write myDate &amp;gt;= yourDate&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As we can see in the above example, Date only needs to implement &amp;lt;.  It gets the other operators for free. &amp;lt;br&amp;gt; [9]&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class (known as the ''diamond problem '' or ''fork-join'' inheritance)&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Does a client or inheritor of a class depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Should common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: How is this problem of Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:Untitled123.jpg|frame|center|Figure 2. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular type-checking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=How mixins solve the Diamond Problem=&lt;br /&gt;
&lt;br /&gt;
Ruby supports only single inheritance, i.e; a given class can inherit from only one parent class. But it is allowed to mixin many modules into a class. Moreover, modules are not permitted to inherit from other modules or classes. As these two conditions have to be always followed, there will be no &amp;quot;diamond&amp;quot; inheritance situation as every class is guaranteed to have a single parent class in case of inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, modules can mixin modules. So the following situation (hierarchy) may arise:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                       module P&lt;br /&gt;
&lt;br /&gt;
                          module Q                    module R&lt;br /&gt;
&lt;br /&gt;
                                       class X&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This hierarchy resembles Diamond Problem. But Ruby's method look-up flow [10: http://phrogz.net/RubyLibs/RubyMethodLookupFlow] guarantees that mixin modules are searched before the parent class. Thus, the order of mixing in is significant in Ruby which guarantees that the module/class to be searched is unambiguous. Hence, the diamond problem is avoided.                               &lt;br /&gt;
&lt;br /&gt;
Though mixins avoid diamond problem, they have a few disadvantages too. The following section does a comparison between the concepts, namely, Multiple Inheritance, Interfaces and Mixins, mentioned above. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison =&lt;br /&gt;
&lt;br /&gt;
== Mixins and Interfaces ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixins and C++ Multiple Inheritance ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
As we saw, each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they do not allow states. Different languages try to deal with this problem in a different way. JavaFX technology has introduced mixins too. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned in the Sun Microsystem website [[http://java.sun.com/developer/technicalArticles/javafx/mixin/]], ''developers have a new style of class inheritance: a mixin. A mixin is a type of class that can be inherited by a subclass, but it is not meant for instantiation. In this manner, mixins are similar to interfaces in the Java programming language. However, the difference is that mixins may actually define method bodies and class variables in addition to constants and method signatures.'' &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, we can conclude that it is the responsibility of the programmer to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
[7] http://codeshooter.wordpress.com/2008/09/27/including-and-extending-modules-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[8] http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&lt;br /&gt;
&lt;br /&gt;
[9] http://hestia.typepad.com/flatlander/2009/01/scala-for-c-programmers-part-1-mixins-and-traits.html&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[5] http://scg.unibe.ch/archive/projects/Reic05a.pdf&lt;br /&gt;
&lt;br /&gt;
[6] http://scg.unibe.ch/archive/phd/schaerli-phd.pdf&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37965</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37965"/>
		<updated>2010-10-14T03:39:14Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Mixin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins = &lt;br /&gt;
&lt;br /&gt;
The concept of '''Inheritance''' and its working is explained in most of the [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) overviews. But a very few of these OOP overviews discuss alternatives when Inheritance fails to be the best design choice for an OO application. The main focus of this topic is to introduce the case where Inheritance proves to be a bad design option and discuss the concepts which are practiced to overcome it. The discussion starts with brief description of what Inheritance is and how it works. The classic scenario of the '''deadly Diamond problem''' is mentioned to discuss why exactly inheritance is criticized. The potential alternatives to solve this problem are introduced. We look at the merits and demerits of these alternatives. This topic discusses [http://en.wikipedia.org/wiki/Mixin/ Mixins] more closely. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in OOP Design ===&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of OOP that facilitates reuse and compartmentalization of code by creating objects (which are collections of attributes and behaviors) that can be derived from existing objects. The deriving object (class) is termed as the ''child class/sub-class/derived class'', and, the object (class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single inheritance creates a [http://en.wikipedia.org/wiki/Is-a/ Is-A] relationship amongst the classes. Consider the concept of single inheritance using class extension feature:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  class Animal{&lt;br /&gt;
      //...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Reptiles extends Animal{ &lt;br /&gt;
      //... &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the Reptiles class is a '''specialization''' of Animals class and derives some/all of its attributes and/or methods from the Animal class.&lt;br /&gt;
&lt;br /&gt;
The next section briefly discusses the alternatives of Inheritance used in various OO languages.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Definitions=&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++, Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent (and not so independent) concepts (classes) into a composite concept called the derived class. For instance, given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following figure depicts the use of multiple inheritance: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:UntitledMI.jpg|frame|upright|center|Figure 1. Multiple Inheritance]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A novel has a certain number of chapters, characters and other properties that can be thought as features of class STORY. It also has features of class BOOK like publisher, a price, a weight and an ISBN number. Thus, Class NOVEL should inherit from both.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is the code snippet showing the above mentioned example: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class STORY{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class BOOK{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class NOVEL : public STORY, public BOOK{         &lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple, object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''')  are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's the example showing how interfaces are used in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     interface Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly();&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
     public class Eagle implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“Eagle flies”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Eagle();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
     public class Duck implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“I cannot fly!!”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Duck();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the interface Bird has a method fly(). The Eagle class and the Duck class implement this method in different ways. Since each class making use of the interface has to implement all the methods of the interface, the methods in the interface can be implemented in different ways by different classes.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specified to be added to a particular class or object. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is an example code snippet showing the use of Modules in Ruby: [7]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   module takes_courses (parameter)&lt;br /&gt;
        def takes&lt;br /&gt;
             puts &amp;quot;student #{parameter} takes course csc517&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    class students&lt;br /&gt;
       include takes_courses                                                       -----&amp;gt; makes the module methods accessible&lt;br /&gt;
            def student_name( param )&lt;br /&gt;
            puts &amp;quot;student_name = #{param}&amp;quot;&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
    student = students.new&lt;br /&gt;
    #In class&lt;br /&gt;
    student.student_name 'john'                  #student name = John&lt;br /&gt;
    &lt;br /&gt;
    # In Module&lt;br /&gt;
    student.takes                                #student John takes course csc517&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example,A module called takes_courses is defined. This module contains a method called takes which  prints the name of the student who has taken the course csc517. The class called students includes the module takes_courses. A new student object is created. This object calls a method (student_name) defined in the class. The output would be &amp;quot;student_name = John &amp;quot;. The same student object is used to call the method takes in the module. This outputs &amp;quot;student John takes course csc 517&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that are closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes. It does not depend on the language syntax. It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
&lt;br /&gt;
Example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
Here, Each element in the array is printed.&lt;br /&gt;
&lt;br /&gt;
Following is an example showing the use of mixins: [8]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  module Draw&lt;br /&gt;
    def  draw_shape&lt;br /&gt;
       puts &amp;quot;draw &amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class ParentClass&lt;br /&gt;
   def shape&lt;br /&gt;
       puts &amp;quot;shape is circle&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class Circle &amp;lt; ParentClass&lt;br /&gt;
      include Draw&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  f = Circle.new&lt;br /&gt;
  f.shape&lt;br /&gt;
  f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example, a subclass circle inherits the methods defined in the parent class.The class circle also includes the drawModule. It can access the methods in the module.A new circle object is created which can access the methods in parent_class as well as the methods defined in the module. The output would be i)shape is a circle ii) draw&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Trait is not an abstract type and implements methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traits can be considered as a combination of interfaces that are abstract and their implementation. Here's a Scala trait representing objects that can be compared and ordered:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trait Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean&lt;br /&gt;
  def &amp;lt;=(that: Any): Boolean = (this &amp;lt; that) || (this == that)&lt;br /&gt;
  def &amp;gt; (that: Any): Boolean = !(this &amp;lt;= that)&lt;br /&gt;
  def &amp;gt;=(that: Any): Boolean = !(this &amp;lt; that)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Date extends Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean = /* implementation */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// can now write myDate &amp;gt;= yourDate&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As we can see in the above example, Date only needs to implement &amp;lt;.  It gets the other operators for free. &amp;lt;br&amp;gt; [9]&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class (known as the ''diamond problem '' or ''fork-join'' inheritance)&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Does a client or inheritor of a class depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Should common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: How is this problem of Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:Untitled123.jpg|frame|center|Figure 2. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular type-checking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=How mixins solve the Diamond Problem=&lt;br /&gt;
&lt;br /&gt;
Ruby supports only single inheritance, i.e; a given class can inherit from only one parent class. But it is allowed to mixin many modules into a class. Moreover, modules are not permitted to inherit from other modules or classes. As these two conditions have to be always followed, there will be no &amp;quot;diamond&amp;quot; inheritance situation as every class is guaranteed to have a single parent class in case of inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, modules can mixin modules. So the following situation (hierarchy) may arise:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                       module P&lt;br /&gt;
&lt;br /&gt;
                          module Q                    module R&lt;br /&gt;
&lt;br /&gt;
                                       class X&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This hierarchy resembles Diamond Problem. But Ruby's method look-up flow [10: http://phrogz.net/RubyLibs/RubyMethodLookupFlow] guarantees that mixin modules are searched before the parent class. Thus, the order of mixing in is significant in Ruby which guarantees that the module/class to be searched is unambiguous. Hence, the diamond problem is avoided.                               &lt;br /&gt;
&lt;br /&gt;
Though mixins avoid diamond problem, they have a few disadvantages too. The following section does a comparison between the concepts, namely, Multiple Inheritance, Interfaces and Mixins, mentioned above. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison =&lt;br /&gt;
&lt;br /&gt;
== Mixins and Interfaces ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixins and C++ Multiple Inheritance ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
As we saw, each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they do not allow states. Different languages try to deal with this problem in a different way. JavaFX technology has introduced mixins too. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned in the Sun Microsystem website [[http://java.sun.com/developer/technicalArticles/javafx/mixin/]], ''developers have a new style of class inheritance: a mixin. A mixin is a type of class that can be inherited by a subclass, but it is not meant for instantiation. In this manner, mixins are similar to interfaces in the Java programming language. However, the difference is that mixins may actually define method bodies and class variables in addition to constants and method signatures.'' &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, we can conclude that it is the responsibility of the programmer to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
[7] http://codeshooter.wordpress.com/2008/09/27/including-and-extending-modules-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[8] http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&lt;br /&gt;
&lt;br /&gt;
[9] http://hestia.typepad.com/flatlander/2009/01/scala-for-c-programmers-part-1-mixins-and-traits.html&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[5] http://scg.unibe.ch/archive/projects/Reic05a.pdf&lt;br /&gt;
&lt;br /&gt;
[6] http://scg.unibe.ch/archive/phd/schaerli-phd.pdf&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37963</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37963"/>
		<updated>2010-10-14T03:14:02Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Mixin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins = &lt;br /&gt;
&lt;br /&gt;
The concept of '''Inheritance''' and its working is explained in most of the [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) overviews. But a very few of these OOP overviews discuss alternatives when Inheritance fails to be the best design choice for an OO application. The main focus of this topic is to introduce the case where Inheritance proves to be a bad design option and discuss the concepts which are practiced to overcome it. The discussion starts with brief description of what Inheritance is and how it works. The classic scenario of the '''deadly Diamond problem''' is mentioned to discuss why exactly inheritance is criticized. The potential alternatives to solve this problem are introduced. We look at the merits and demerits of these alternatives. This topic discusses [http://en.wikipedia.org/wiki/Mixin/ Mixins] more closely. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in OOP Design ===&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of OOP that facilitates reuse and compartmentalization of code by creating objects (which are collections of attributes and behaviors) that can be derived from existing objects. The deriving object (class) is termed as the ''child class/sub-class/derived class'', and, the object (class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single inheritance creates a [http://en.wikipedia.org/wiki/Is-a/ Is-A] relationship amongst the classes. Consider the concept of single inheritance using class extension feature:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  class Animal{&lt;br /&gt;
      //...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Reptiles extends Animal{ &lt;br /&gt;
      //... &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the Reptiles class is a '''specialization''' of Animals class and derives some/all of its attributes and/or methods from the Animal class.&lt;br /&gt;
&lt;br /&gt;
The next section briefly discusses the alternatives of Inheritance used in various OO languages.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Definitions=&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++, Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent (and not so independent) concepts (classes) into a composite concept called the derived class. For instance, given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following figure depicts the use of multiple inheritance: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:UntitledMI.jpg|frame|upright|center|Figure 1. Multiple Inheritance]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A novel has a certain number of chapters, characters and other properties that can be thought as features of class STORY. It also has features of class BOOK like publisher, a price, a weight and an ISBN number. Thus, Class NOVEL should inherit from both.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is the code snippet showing the above mentioned example: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class STORY{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class BOOK{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class NOVEL : public STORY, public BOOK{         &lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple, object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''')  are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's the example showing how interfaces are used in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     interface Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly();&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
     public class Eagle implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“Eagle flies”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Eagle();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
     public class Duck implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“I cannot fly!!”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Duck();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the interface Bird has a method fly(). The Eagle class and the Duck class implement this method in different ways. Since each class making use of the interface has to implement all the methods of the interface, the methods in the interface can be implemented in different ways by different classes.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specified to be added to a particular class or object. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is an example code snippet showing the use of Modules in Ruby: [7]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   module takes_courses (parameter)&lt;br /&gt;
        def takes&lt;br /&gt;
             puts &amp;quot;student #{parameter} takes course csc517&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    class students&lt;br /&gt;
       include takes_courses                                                       -----&amp;gt; makes the module methods accessible&lt;br /&gt;
            def student_name( param )&lt;br /&gt;
            puts &amp;quot;student_name = #{param}&amp;quot;&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
    student = students.new&lt;br /&gt;
    #In class&lt;br /&gt;
    student.student_name 'john'                  #student name = John&lt;br /&gt;
    &lt;br /&gt;
    # In Module&lt;br /&gt;
    student.takes                                #student John takes course csc517&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example,A module called takes_courses is defined. This module contains a method called takes which  prints the name of the student who has taken the course csc517. The class called students includes the module takes_courses. A new student object is created. This object calls a method (student_name) defined in the class. The output would be &amp;quot;student_name = John &amp;quot;. The same student object is used to call the method takes in the module. This outputs &amp;quot;student John takes course csc 517&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that are closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes. It does not depend on the language syntax. It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
&lt;br /&gt;
Example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
Here, Each element in the array is printed.&lt;br /&gt;
&lt;br /&gt;
Following is an example showing the use of mixins: [8]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  module drawModule&lt;br /&gt;
    def  draw_shape&lt;br /&gt;
       puts ”draw”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class parentClass&lt;br /&gt;
   def shape&lt;br /&gt;
       puts “shape is circle”&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class circle &amp;lt; parentClass&lt;br /&gt;
      include drawModule&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  f = circle.new&lt;br /&gt;
  f.parentClass &lt;br /&gt;
  f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above example, a subclass circle inherits the methods defined in the parent class.The class circle also includes the drawModule. It can access the methods in the module.A new circle object is created which can access the methods in parent_class as well as the methods defined in the module&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Trait is not an abstract type and implements methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traits can be considered as a combination of interfaces that are abstract and their implementation. Here's a Scala trait representing objects that can be compared and ordered:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trait Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean&lt;br /&gt;
  def &amp;lt;=(that: Any): Boolean = (this &amp;lt; that) || (this == that)&lt;br /&gt;
  def &amp;gt; (that: Any): Boolean = !(this &amp;lt;= that)&lt;br /&gt;
  def &amp;gt;=(that: Any): Boolean = !(this &amp;lt; that)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Date extends Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean = /* implementation */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// can now write myDate &amp;gt;= yourDate&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As we can see in the above example, Date only needs to implement &amp;lt;.  It gets the other operators for free. &amp;lt;br&amp;gt; [9]&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class (known as the ''diamond problem '' or ''fork-join'' inheritance)&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Does a client or inheritor of a class depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Should common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: How is this problem of Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:Untitled123.jpg|frame|center|Figure 2. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular type-checking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=How mixins solve the Diamond Problem=&lt;br /&gt;
&lt;br /&gt;
Ruby supports only single inheritance, i.e; a given class can inherit from only one parent class. But it is allowed to mixin many modules into a class. Moreover, modules are not permitted to inherit from other modules or classes. As these two conditions have to be always followed, there will be no &amp;quot;diamond&amp;quot; inheritance situation as every class is guaranteed to have a single parent class in case of inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, modules can mixin modules. So the following situation (hierarchy) may arise:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                       module P&lt;br /&gt;
&lt;br /&gt;
                          module Q                    module R&lt;br /&gt;
&lt;br /&gt;
                                       class X&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This hierarchy resembles Diamond Problem. But Ruby's method look-up flow [10: http://phrogz.net/RubyLibs/RubyMethodLookupFlow] guarantees that mixin modules are searched before the parent class. Thus, the order of mixing in is significant in Ruby which guarantees that the module/class to be searched is unambiguous. Hence, the diamond problem is avoided.                               &lt;br /&gt;
&lt;br /&gt;
Though mixins avoid diamond problem, they have a few disadvantages too. The following section does a comparison between the concepts, namely, Multiple Inheritance, Interfaces and Mixins, mentioned above. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison =&lt;br /&gt;
&lt;br /&gt;
== Mixins and Interfaces ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixins and C++ Multiple Inheritance ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
As we saw, each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they do not allow states. Different languages try to deal with this problem in a different way. JavaFX technology has introduced mixins too. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned in the Sun Microsystem website [[http://java.sun.com/developer/technicalArticles/javafx/mixin/]], ''developers have a new style of class inheritance: a mixin. A mixin is a type of class that can be inherited by a subclass, but it is not meant for instantiation. In this manner, mixins are similar to interfaces in the Java programming language. However, the difference is that mixins may actually define method bodies and class variables in addition to constants and method signatures.'' &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, we can conclude that it is the responsibility of the programmer to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
[7] http://codeshooter.wordpress.com/2008/09/27/including-and-extending-modules-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[8] http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&lt;br /&gt;
&lt;br /&gt;
[9] http://hestia.typepad.com/flatlander/2009/01/scala-for-c-programmers-part-1-mixins-and-traits.html&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[5] http://scg.unibe.ch/archive/projects/Reic05a.pdf&lt;br /&gt;
&lt;br /&gt;
[6] http://scg.unibe.ch/archive/phd/schaerli-phd.pdf&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37924</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37924"/>
		<updated>2010-10-14T02:00:30Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: /* Modules */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins = &lt;br /&gt;
&lt;br /&gt;
The concept of '''Inheritance''' and its working is explained in most of the [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) overviews. But a very few of these OOP overviews discuss alternatives when Inheritance fails to be the best design choice for an OO application. The main focus of this topic is to introduce the case where Inheritance proves to be a bad design option and discuss the concepts which are practiced to overcome it. The discussion starts with brief description of what Inheritance is and how it works. The classic scenario of the '''deadly Diamond problem''' is mentioned to discuss why exactly inheritance is criticized. The potential alternatives to solve this problem are introduced. We look at the merits and demerits of these alternatives. This topic discusses [http://en.wikipedia.org/wiki/Mixin/ Mixins] more closely. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Inheritance in OOP Design ===&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of OOP that facilitates reuse and compartmentalization of code by creating objects (which are collections of attributes and behaviors) that can be derived from existing objects. The deriving object (class) is termed as the ''child class/sub-class/derived class'', and, the object (class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Single inheritance creates a [http://en.wikipedia.org/wiki/Is-a/ Is-A] relationship amongst the classes. Consider the concept of single inheritance using class extension feature:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
  class Animal{&lt;br /&gt;
      //...&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  class Reptiles extends Animal{ &lt;br /&gt;
      //... &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the Reptiles class is a '''specialization''' of Animals class and derives some/all of its attributes and/or methods from the Animal class.&lt;br /&gt;
&lt;br /&gt;
The next section briefly discusses the alternatives of Inheritance used in various OO languages.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Definitions=&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++, Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent (and not so independent) concepts (classes) into a composite concept called the derived class. For instance, given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following figure depicts the use of multiple inheritance: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:UntitledMI.jpg|frame|upright|center|Figure 1. Multiple Inheritance]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A novel has a certain number of chapters, characters and other properties that can be thought as features of class STORY. It also has features of class BOOK like publisher, a price, a weight and an ISBN number. Thus, Class NOVEL should inherit from both.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is the code snippet showing the above mentioned example: &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
   class STORY{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class BOOK{&lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   class NOVEL : public STORY, public BOOK{         &lt;br /&gt;
     //...&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple, object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''')  are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here's the example showing how interfaces are used in Java:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
     interface Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly();&lt;br /&gt;
       }&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
     public class Eagle implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“Eagle flies”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Eagle();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
     public class Duck implements Bird &lt;br /&gt;
       {&lt;br /&gt;
          public void fly()&lt;br /&gt;
           { &lt;br /&gt;
               System.out.println(“I cannot fly!!”);&lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
          public  static void main(String args[])&lt;br /&gt;
           {&lt;br /&gt;
               Bird  bird = new Duck();&lt;br /&gt;
               bird.fly();&lt;br /&gt;
           }&lt;br /&gt;
       }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example, the interface Bird has a method fly(). The Eagle class and the Duck class implement this method in different ways. Since each class making use of the interface has to implement all the methods of the interface, the methods in the interface can be implemented in different ways by different classes.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specified to be added to a particular class or object. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Following is an example code snippet showing the use of Modules in Ruby: [7]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
   module takes_courses (parameter)&lt;br /&gt;
        def takes&lt;br /&gt;
             puts &amp;quot;student #{parameter} takes course csc517&amp;quot;&lt;br /&gt;
        end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    class students&lt;br /&gt;
       include takes_courses                                                       -----&amp;gt; makes the module methods accessible&lt;br /&gt;
            def student_name( param )&lt;br /&gt;
            puts &amp;quot;student_name = #{param}&amp;quot;&lt;br /&gt;
       end&lt;br /&gt;
     end&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
    student = students.new&lt;br /&gt;
    #In class&lt;br /&gt;
    student.student_name 'john'                  #student name = John&lt;br /&gt;
    &lt;br /&gt;
    # In Module&lt;br /&gt;
    student.takes                                #student John takes course csc517&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the above example,A module called takes_courses is defined. This module contains a method called takes which  prints the name of the student who has taken the course csc517. The class called students includes the module takes_courses. A new student object is created. This object calls a method (student_name) defined in the class. The output would be &amp;quot;student_name = John &amp;quot;. The same student object is used to call the method takes in the module. This outputs &amp;quot;student John takes course csc 517&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that are closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes. It does not depend on the language syntax. It is defined as “ an operation that is performed on elements of a set”.Example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
Following is an example showing the use of mixins: [8]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  module drawModule&lt;br /&gt;
    def  draw_shape&lt;br /&gt;
       puts ”draw”&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  class parentClass&lt;br /&gt;
   def shape&lt;br /&gt;
       puts “shape is circle”&lt;br /&gt;
   end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  class circle &amp;lt; parentClass&lt;br /&gt;
      include drawModule&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  f = circle.new&lt;br /&gt;
  f.parentClass&lt;br /&gt;
  f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Trait is not an abstract type and implements methods. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Traits can be considered as a combination of interfaces that are abstract and their implementation. Here's a Scala trait representing objects that can be compared and ordered:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
trait Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean&lt;br /&gt;
  def &amp;lt;=(that: Any): Boolean = (this &amp;lt; that) || (this == that)&lt;br /&gt;
  def &amp;gt; (that: Any): Boolean = !(this &amp;lt;= that)&lt;br /&gt;
  def &amp;gt;=(that: Any): Boolean = !(this &amp;lt; that)&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Date extends Ord {&lt;br /&gt;
  def &amp;lt; (that: Any): Boolean = /* implementation */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// can now write myDate &amp;gt;= yourDate&lt;br /&gt;
&amp;lt;/pre&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As we can see in the above example, Date only needs to implement &amp;lt;.  It gets the other operators for free. &amp;lt;br&amp;gt; [9]&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class (known as the ''diamond problem '' or ''fork-join'' inheritance)&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Does a client or inheritor of a class depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Should common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: How is this problem of Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:Untitled123.jpg|frame|center|Figure 2. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular type-checking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=How mixins solve the Diamond Problem=&lt;br /&gt;
&lt;br /&gt;
Ruby supports only single inheritance, i.e; a given class can inherit from only one parent class. But it is allowed to mixin many modules into a class. Moreover, modules are not permitted to inherit from other modules or classes. As these two conditions have to be always followed, there will be no &amp;quot;diamond&amp;quot; inheritance situation as every class is guaranteed to have a single parent class in case of inheritance.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned above, modules can mixin modules. So the following situation (hierarchy) may arise:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                                       module P&lt;br /&gt;
&lt;br /&gt;
                          module Q                    module R&lt;br /&gt;
&lt;br /&gt;
                                       class X&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This hierarchy resembles Diamond Problem. But Ruby's method look-up flow [10: http://phrogz.net/RubyLibs/RubyMethodLookupFlow] guarantees that mixin modules are searched before the parent class. Thus, the order of mixing in is significant in Ruby which guarantees that the module/class to be searched is unambiguous. Hence, the diamond problem is avoided.                               &lt;br /&gt;
&lt;br /&gt;
Though mixins avoid diamond problem, they have a few disadvantages too. The following section does a comparison between the concepts, namely, Multiple Inheritance, Interfaces and Mixins, mentioned above. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Comparison =&lt;br /&gt;
&lt;br /&gt;
== Mixins and Interfaces ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Mixins and C++ Multiple Inheritance ==&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible. &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
As we saw, each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they do not allow states. Different languages try to deal with this problem in a different way. JavaFX technology has introduced mixins too. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As mentioned in the Sun Microsystem website [[http://java.sun.com/developer/technicalArticles/javafx/mixin/]], ''developers have a new style of class inheritance: a mixin. A mixin is a type of class that can be inherited by a subclass, but it is not meant for instantiation. In this manner, mixins are similar to interfaces in the Java programming language. However, the difference is that mixins may actually define method bodies and class variables in addition to constants and method signatures.'' &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Thus, we can conclude that it is the responsibility of the programmer to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
[7] http://codeshooter.wordpress.com/2008/09/27/including-and-extending-modules-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[8] http://www.innovationontherun.com/why-rubys-mixins-gives-rails-an-advantage-over-java-frameworks/&lt;br /&gt;
&lt;br /&gt;
[9] http://hestia.typepad.com/flatlander/2009/01/scala-for-c-programmers-part-1-mixins-and-traits.html&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[5] http://scg.unibe.ch/archive/projects/Reic05a.pdf&lt;br /&gt;
&lt;br /&gt;
[6] http://scg.unibe.ch/archive/phd/schaerli-phd.pdf&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37201</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37201"/>
		<updated>2010-10-06T20:49:35Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins =&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the ''child class/sub-class/derived class'' , and, the object(class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++,Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes.&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple,object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''') are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that is closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes.&lt;br /&gt;
Its does not depend on the language syntax.&lt;br /&gt;
It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods. &lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class ( known as the ''diamond problem '' or ''fork-join'' inheritance )&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Is it allowed for a client or inheritor of a class to depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Must the common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation. How is this problem dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:DiamondProblem.jpg|frame|center|Figure 1. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&lt;br /&gt;
&lt;br /&gt;
== Comparison ==&lt;br /&gt;
&lt;br /&gt;
=== Mixins and Interfaces ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins and C++ Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they don not allow states. The programmer has to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37200</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37200"/>
		<updated>2010-10-06T20:48:46Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins =&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the ''child class/sub-class/derived class'' , and, the object(class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++,Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes.&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple,object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''') are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
 module drawModule&lt;br /&gt;
    Def  draw_shape&lt;br /&gt;
      Puts ”draw”&lt;br /&gt;
     End&lt;br /&gt;
 End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Class Parentclass&lt;br /&gt;
  Def shape &lt;br /&gt;
   Puts “shape is circle”&lt;br /&gt;
 End&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Class circle &amp;lt; parentclass&lt;br /&gt;
 Include drawModule&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
f = circle.new&lt;br /&gt;
f.parentclass&lt;br /&gt;
f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that is closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes.&lt;br /&gt;
Its does not depend on the language syntax.&lt;br /&gt;
It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods. &lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class ( known as the ''diamond problem '' or ''fork-join'' inheritance )&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Is it allowed for a client or inheritor of a class to depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Must the common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation. How is this problem dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:DiamondProblem.jpg|frame|center|Figure 1. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&lt;br /&gt;
&lt;br /&gt;
== Comparison ==&lt;br /&gt;
&lt;br /&gt;
=== Mixins and Interfaces ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins and C++ Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they don not allow states. The programmer has to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37197</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37197"/>
		<updated>2010-10-06T20:47:34Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins =&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the ''child class/sub-class/derived class'' , and, the object(class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++,Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes.&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple,object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''') are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
module drawModule&lt;br /&gt;
 Def  draw_shape&lt;br /&gt;
    Puts ”draw”&lt;br /&gt;
    End&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Class Parentclass&lt;br /&gt;
Def shape&lt;br /&gt;
Puts “shape is circle”&lt;br /&gt;
End&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Class circle &amp;lt; parentclass&lt;br /&gt;
 Include drawModule&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
f = circle.new&lt;br /&gt;
f.parentclass&lt;br /&gt;
f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that is closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes.&lt;br /&gt;
Its does not depend on the language syntax.&lt;br /&gt;
It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods. &lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class ( known as the ''diamond problem '' or ''fork-join'' inheritance )&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Is it allowed for a client or inheritor of a class to depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Must the common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation. How is this problem dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:DiamondProblem.jpg|frame|center|Figure 1. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&lt;br /&gt;
&lt;br /&gt;
== Comparison ==&lt;br /&gt;
&lt;br /&gt;
=== Mixins and Interfaces ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins and C++ Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they don not allow states. The programmer has to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37196</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37196"/>
		<updated>2010-10-06T20:46:00Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins =&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the ''child class/sub-class/derived class'' , and, the object(class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++,Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes.&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple,object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''') are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
module drawModule&lt;br /&gt;
 Def  draw_shape&lt;br /&gt;
    Puts ”draw”&lt;br /&gt;
    End&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
Class Parentclass&lt;br /&gt;
Def shape&lt;br /&gt;
Puts “shape is circle”&lt;br /&gt;
End&lt;br /&gt;
End&lt;br /&gt;
&lt;br /&gt;
Class circle &amp;lt; parentclass&lt;br /&gt;
 Include drawModule&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
f = circle.new&lt;br /&gt;
f.parentclass&lt;br /&gt;
f.draw_shape&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that is closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes.&lt;br /&gt;
Its does not depend on the language syntax.&lt;br /&gt;
It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods. &lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class ( known as the ''diamond problem '' or ''fork-join'' inheritance )&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Is it allowed for a client or inheritor of a class to depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Must the common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation. How is this problem dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:DiamondProblem.jpg|frame|center|Figure 1. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&lt;br /&gt;
&lt;br /&gt;
== Comparison ==&lt;br /&gt;
&lt;br /&gt;
=== Mixins and Interfaces ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins and C++ Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they don not allow states. The programmer has to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37185</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3b sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3b_sv&amp;diff=37185"/>
		<updated>2010-10-06T20:25:14Z</updated>

		<summary type="html">&lt;p&gt;Vsubram3: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Multiple Inheritance and Mixins =&lt;br /&gt;
&lt;br /&gt;
'''Inheritance''' is a concept of [http://en.wikipedia.org/wiki/Object-oriented_programming/ Object Oriented Programming] (OOP) that facilitates reuse and compartmentalization of code by creating objects ( which are collections of attributes and behaviors ) which can be derived from existing objects. The deriving object(class) is termed as the ''child class/sub-class/derived class'' , and, the object(class) whose methods are being used is called the ''parent class/super-class/base class''. This relationship between the super and sub-classes arising due to inheritance creates a hierarchy amongst the classes. Inheritance is termed as '' Single Inheritance'' if there is only one base class. Languages like C++,Java,Ruby,Python and SmallTalk support inheritance.&lt;br /&gt;
&lt;br /&gt;
==Definitions==&lt;br /&gt;
&lt;br /&gt;
===Multiple Inheritance===&lt;br /&gt;
&lt;br /&gt;
If a child class inherits behaviors and attributes from two or more parent classes, it is termed as ''Multiple Inheritance''. Languages supporting this feature include C++,Perl and Python. The main advantage of ''Multiple Inheritance'' is that it enables an user to combine independent(and not so independent) concepts(classes) into a composite concept called the derived class. For instance,given a set of ''interaction'' base classes and a set of base classes defining ''display options'', the user can create a new window by selecting a style of window interaction and appearance from the given base classes.&lt;br /&gt;
&lt;br /&gt;
Suppose there are '''N''' concepts, each of which can be combined with one of '''M''' other concepts, '''N+M''' classes are required to represent all the combined concepts. The situation with N = M = 1 is Single Inheritance. Examples with N&amp;gt;=2 and M&amp;gt;=2 are common and Multiple Inheritance plays an important role in such situations by avoiding replication.&lt;br /&gt;
&lt;br /&gt;
===Interfaces===&lt;br /&gt;
&lt;br /&gt;
Java designers cut the features of multiple inheritance and operator overloading from C++ to keep the language ''simple,object oriented and familiar''. '''Interfaces''' were introduced in Java to simulate multiple inheritance. Interfaces (defined using the keyword '''interface''') are [http://en.wikipedia.org/wiki/Abstract_type/ abstract type] containing only the method signatures and constant declarations. A class which implements an interface does so by implementing all its methods. Thus, interfaces act as templates.&lt;br /&gt;
&lt;br /&gt;
===Modules===&lt;br /&gt;
&lt;br /&gt;
A module is defined as a component which regroups similar things. Modules differ from classes as modules are just names to regroup things which we think are similar, whereas, classes consist of variables and methods describing the state and the behavior of an entity [1]. A module &amp;quot;protects&amp;quot; the names it contains. The primary benefits of using modules are:&lt;br /&gt;
&lt;br /&gt;
* The code is more organized.&lt;br /&gt;
* The risk of name clashes is eliminated as modules provide '''namespace''' facility.&lt;br /&gt;
* Mixins can be implemented using modules.&lt;br /&gt;
&lt;br /&gt;
Unlike classes, objects can neither be created nor sub-classed based on modules. Instead, the functionality of a particular module can be specifies to be added to a particular class or object.&lt;br /&gt;
&lt;br /&gt;
===Mixin===&lt;br /&gt;
&lt;br /&gt;
Mixins are one of the defining features of Ruby that is closely related to ''Duck typing''. Duck typing is a technique that allows usage of objects to respond to a certain set of method calls interchangeably. Mixins are a group of methods that are not yet attached to any class. They exist in a module, and are useful only when they are included in a class. Mixin thus behaves as an interface with implemented methods. &lt;br /&gt;
&lt;br /&gt;
Mixins cannot be instantiated, but, provide functionality to be inherited by a subclass. Ruby supports only single inheritance. So, mixins prove to be of great importance in Ruby as they simulate multiple inheritance capability in Ruby which allows classes to inherit functionalities from two or more mixins [2],[3],[4].&lt;br /&gt;
&lt;br /&gt;
An Enumeration is a type of mixin module which can be used by all the classes.&lt;br /&gt;
Its does not depend on the language syntax.&lt;br /&gt;
It is defined as “ an operation that is performed on elements of a set”.&lt;br /&gt;
example of enumeration is:&lt;br /&gt;
[34, 56, 76, 96].each {|a| puts a}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Traits===&lt;br /&gt;
&lt;br /&gt;
A ''trait'' can be considered as a collection of methods which serves as a simple model for structured object oriented programs [5]. The main difference between mixins and traits lies in the way both are composed. Mixins are composed using only the inheritance operation, but, traits can include symmetric sum, aliasing and method exclusion. Traits is not an abstract type and implements methods. &lt;br /&gt;
&lt;br /&gt;
With [http://www.iam.unibe.ch/~scg/Research/Traits/ Traits], classes are organized in a single inheritance hierarchy. Traits are basically used to specify incremental differences in the behavior of sub- and super-classes. Traits are supported in programming languages like Fortress,Perl,SmallTalk and Joose framework for JavaScript.&lt;br /&gt;
&lt;br /&gt;
= Diamond Problem : Loophole in Multiple Inheritance =&lt;br /&gt;
&lt;br /&gt;
With single inheritance, classes cannot be classified into the branches of the hierarchy and hence, is limiting. Multiple inheritance overcomes this and supports complex relationship models.&lt;br /&gt;
&lt;br /&gt;
But multiple inheritance has the following problems:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* It is possible that a class inherits multiple features with the same name.&lt;br /&gt;
* There can be more than one path to a given ancestor from a class ( known as the ''diamond problem '' or ''fork-join'' inheritance )&amp;lt;br&amp;gt;&lt;br /&gt;
* Encapsulation of inheritance: Is it allowed for a client or inheritor of a class to depend on the inheritance structure of that class?&lt;br /&gt;
* Common ancestor duplication problem: Must the common ancestor be duplicated or not?&lt;br /&gt;
* Duplicate parent operation invocation problem: Multiple inheriting from two classes that both invoke the same parent operation on a common ancestor can cause duplicate invocation of this parent operation. How is this problem dealt with?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The conflicting-features problem can be resolved by permitting renaming or by having a linear class hierarchy. The ''diamond problem'' is an ambiguity arising when two classes B and C inherit from class A, and both B and C are inherited by class D. If both classes B and C override the definition of a method inherited from A, then, when class D inherits this same method, conflict arises if D should use the method from B or C.&lt;br /&gt;
&lt;br /&gt;
[[Image:DiamondProblem.jpg|frame|center|Figure 1. The diamond problem hierarchy]]&lt;br /&gt;
&lt;br /&gt;
In '''C++''', Virtual inheritance was proposed as one solution for class D to inherit only one copy of A's methods. But object initialization is a problem when we have only one copy. Another consequence of diamond problem is that multiple inheritance interacts poorly with modular typechecking of [http://en.wikipedia.org/wiki/Multiple_dispatch/ multiple dispatch].&lt;br /&gt;
&lt;br /&gt;
'''Java''' makes use of the concept of interfaces to simulate Multiple Inheritance and the responsibility of implementing the functions defined in the interfaces lies with the classes making use of the interface. Similarly '''Ruby''' uses the concept of mixins and '''SmallTalk''' and '''Perl''' use Traits. But each solution has its own pros and cons.&lt;br /&gt;
&lt;br /&gt;
== Comparison ==&lt;br /&gt;
&lt;br /&gt;
=== Mixins and Interfaces ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|FEATURE&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|MIXINS&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|JAVA INTERFACES   &lt;br /&gt;
|- &lt;br /&gt;
|Maintainability&lt;br /&gt;
|In Ruby, modules have the implementation of the methods. So all the classes who “include” the module get the same method and its functionality. The definition of the method can change at run time.&lt;br /&gt;
|Java forces the classes using the interfaces to implement the methods defined in the interface. This might lead to code duplication if two classes have similar functionality. Secondly, changes have to be made at both places if the method changes in future.&lt;br /&gt;
|-&lt;br /&gt;
|Size of the Code&lt;br /&gt;
|The class/objects automatically get access to the methods of the included module(s).&lt;br /&gt;
|If a class inherits multiple interfaces, it is forced to implement unnecessary, and sometimes, duplicate code which makes the code grow in size.&lt;br /&gt;
|-&lt;br /&gt;
|Readability&lt;br /&gt;
|The module and the class using the module can be in different files. Hence, the code is less readable. So mixins are beneficial for individual or small teams. For bigger teams, documentation becomes an important criterion. &lt;br /&gt;
|The code is more readable and explicit as the implementation of a method can be found at a place.&lt;br /&gt;
|-&lt;br /&gt;
|Naming Conflicts&lt;br /&gt;
|Modules provide the facility of namespace. So it is possible for a class to inherit two or more methods of same name from different modules.&lt;br /&gt;
|Since the class itself has to provide the implementation of the methods, inheriting methods of same names from different interfaces will signal an error message.  &lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Mixins and C++ Multiple Inheritance ===&lt;br /&gt;
&lt;br /&gt;
{|cellspacing=&amp;quot;0&amp;quot; border=&amp;quot;2&amp;quot; style=&amp;quot;color:black; background-color:LightYellow;&amp;quot;&lt;br /&gt;
!style=&amp;quot;width:15%&amp;quot;|&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|ADVANTAGES&lt;br /&gt;
!style=&amp;quot;width:25%&amp;quot;|DISADVANTAGES&lt;br /&gt;
|- &lt;br /&gt;
|MIXINS&lt;br /&gt;
|1. Avoids complex features like virtual base classes.&amp;lt;br&amp;gt;2. Code is less verbose.&amp;lt;br&amp;gt;3. Modules can be made aware of who included them.&lt;br /&gt;
|1. In case of naming conflicts while mixing multiple modules, notification of the error may be incorrect.&amp;lt;br&amp;gt;2. Ruby is used for implementation inheritance and finds very less use in inheritance implementation.&amp;lt;br&amp;gt;3. Since it supports non-virtual classes, getting two copies of the base class requires odd code.&lt;br /&gt;
|-&lt;br /&gt;
|C++ MULTIPLE INHERITANCE&lt;br /&gt;
|1. Naming conflicts point to the proper error normally.&amp;lt;br&amp;gt;2. Can be used for both interface and implementation inheritance.&amp;lt;br&amp;gt;3. Supports virtual base classes.&lt;br /&gt;
|1. C++ classes do not have any knowledge of who inherited them.&amp;lt;br&amp;gt;2. Poses diamond problem&lt;br /&gt;
|-&lt;br /&gt;
&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In the classic scenario of diamond problem [ Figure 1 ], Ruby simplifies the inheritance to look like D&amp;lt;C&amp;lt;B&amp;lt;A. In C++ Multiple Inheritance, C::foo cannot call B::foo, but with Ruby mix-ins,this is possible.&lt;br /&gt;
&lt;br /&gt;
= Conclusion =&lt;br /&gt;
&lt;br /&gt;
Each of the above approaches- Multiple Inheritance, Interfaces, Mixins and Traits - have their own pros and cons. Each one tries to solve the deadly ''Diamond problem'' in a different way, but, no one solves it completely. Traits have the disadvantage that they don not allow states. The programmer has to make the choice of which mechanism fits the best depending upon the task under consideration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
[1] http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html&lt;br /&gt;
&lt;br /&gt;
[2] http://juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;br /&gt;
&lt;br /&gt;
[3] http://ruby.about.com/od/beginningruby/a/mixin.htm&lt;br /&gt;
&lt;br /&gt;
[4] http://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful&lt;br /&gt;
&lt;br /&gt;
[5] http://en.wikipedia.org/wiki/Trait_%28computer_science%29&lt;br /&gt;
&lt;br /&gt;
[6] http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
[1] http://www.oreillynet.com/ruby/blog/2007/01/digging_deep_mixing_it_up_or_i_1.html Article from O'Reilly's website which discusses mixins at depth&lt;br /&gt;
&lt;br /&gt;
[2] http://www.sapphiresteel.com/The-Little-Book-Of-Ruby&lt;br /&gt;
&lt;br /&gt;
[3] http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 &lt;br /&gt;
&lt;br /&gt;
[4] http://www.juixe.com/techknow/index.php/2006/06/15/mixins-in-ruby/&lt;/div&gt;</summary>
		<author><name>Vsubram3</name></author>
	</entry>
</feed>