<?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=Svellan</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=Svellan"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Svellan"/>
	<updated>2026-05-15T10:08:38Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54280</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54280"/>
		<updated>2011-10-29T18:12:41Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated, provide a global point of access to that object&lt;br /&gt;
! Convert the interface of a class into one that the client expects &lt;br /&gt;
! Encapsulate a request in an object and allow the parameterization of clients with different requests&lt;br /&gt;
! Encapsulate a set of algorithms and use them interchangeably.&lt;br /&gt;
|- &lt;br /&gt;
! Advantages&lt;br /&gt;
! Helps achieve serialization and is useful in scenarios of logging, communication and lazy instantiations&lt;br /&gt;
! Enables classes to communicate which otherwise would not be able to due to incompatible interfaces.&lt;br /&gt;
! Addition of new functionality is fairly simple as it just calls for encapsulating the functionality into the Command Object.&lt;br /&gt;
! Large conditional statements are eliminated which makes it easy to keep track of the different behaviors which are now in separate classes.&lt;br /&gt;
|- &lt;br /&gt;
! Disadvantages&lt;br /&gt;
! Brings in the concept of global state, making unit testing difficult. Also reduces the scope of parallelism within the program.&lt;br /&gt;
! When using Object Adapters, all the code for delegating all the necessary requests to the Adaptee has to be written. &lt;br /&gt;
! The increase in the number of Command Classes, clutters up the design. &lt;br /&gt;
! The increase in the number of objects, and all the algorithms use the same interface.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/viewer?a=v&amp;amp;q=cache:UGImuHnhO8MJ:www.cs.toronto.edu/~arnold/407/06s/lectures/studentPresentations/stateStrategy/state_strat_pres.ppt+disadvantages+of+strategy+pattern&amp;amp;hl=en&amp;amp;gl=us&amp;amp;pid=bl&amp;amp;srcid=ADGEEShbX8h_8wQ7PkIoXMKwXjPgISI7dduhY-HrO8fUV8lntQw0RYYWQ-eG_hLAxTT3pjUj5j5Dj8F7-baC3D-C6Znv_YTujBdB_E6-WkZ9KMe2jWXGtZgeRtMcwEJtpqN8JNOC9itc&amp;amp;sig=AHIEtbSP_rpR12IcGL1KyQAhi0PGeUQqrQ&amp;amp;pli=1 9. Command and Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://www.codeproject.com/KB/architecture/commandpatterndemo.aspx 9. Command Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54279</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54279"/>
		<updated>2011-10-29T18:11:59Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated, provide a global point of access to that object&lt;br /&gt;
! Convert the interface of a class into one that the client expects &lt;br /&gt;
! Encapsulate a request in an object and allow the parameterization of clients with different requests&lt;br /&gt;
! Encapsulate a set of algorithms and use them interchangeably.&lt;br /&gt;
|- &lt;br /&gt;
! Advantages&lt;br /&gt;
! Helps achieve serialization and is useful in scenarios of logging, communication and lazy instantiations&lt;br /&gt;
! Enables classes to communicate which otherwise would not be able to due to incompatible interfaces.&lt;br /&gt;
! Addition of new functionality is fairly simple as it just calls for encapsulating the functionality into the Command Object.&lt;br /&gt;
! Large conditional statements are eliminated which makes it easy to keep track of the different behaviors which are now in separate classes.&lt;br /&gt;
|- &lt;br /&gt;
! Disadvantages&lt;br /&gt;
! Brings in the concept of global state, making unit testing difficult. Also reduces the scope of parallelism within the program.&lt;br /&gt;
! When using Object Adapters, all the code for delegating all the necessary requests to the Adaptee has to be written. &lt;br /&gt;
! The increase in the number of Command Classes, clutters up the design. &lt;br /&gt;
! The increase in the number of objects, and all the algorithms use the same interface.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;br /&gt;
&lt;br /&gt;
[https://docs.google.com/a/ncsu.edu/viewer?a=v&amp;amp;q=cache:UGImuHnhO8MJ:www.cs.toronto.edu/~arnold/407/06s/lectures/studentPresentations/stateStrategy/state_strat_pres.ppt+disadvantages+of+strategy+pattern&amp;amp;hl=en&amp;amp;gl=us&amp;amp;pid=bl&amp;amp;srcid=ADGEEShbX8h_8wQ7PkIoXMKwXjPgISI7dduhY-HrO8fUV8lntQw0RYYWQ-eG_hLAxTT3pjUj5j5Dj8F7-baC3D-C6Znv_YTujBdB_E6-WkZ9KMe2jWXGtZgeRtMcwEJtpqN8JNOC9itc&amp;amp;sig=AHIEtbSP_rpR12IcGL1KyQAhi0PGeUQqrQ&amp;amp;pli=1 9. Command and Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54278</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54278"/>
		<updated>2011-10-29T18:08:56Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated, provide a global point of access to that object&lt;br /&gt;
! Convert the interface of a class into one that the client expects &lt;br /&gt;
! Encapsulate a request in an object and allow the parameterization of clients with different requests&lt;br /&gt;
! Encapsulate a set of algorithms and use them interchangeably.&lt;br /&gt;
|- &lt;br /&gt;
! Advantages&lt;br /&gt;
! Helps achieve serialization and is useful in scenarios of logging, communication and lazy instantiations&lt;br /&gt;
! Enables classes to communicate which otherwise would not be able to due to incompatible interfaces.&lt;br /&gt;
! Addition of new functionality is fairly simple as it just calls for encapsulating the functionality into the Command Object.&lt;br /&gt;
! Large conditional statements are eliminated which makes it easy to keep track of the different behaviors which are now in separate classes.&lt;br /&gt;
|- &lt;br /&gt;
! Disadvantages&lt;br /&gt;
! Brings in the concept of global state, making unit testing difficult. Also reduces the scope of parallelism within the program.&lt;br /&gt;
! When using Object Adapters, all the code for delegating all the necessary requests to the Adaptee has to be written. &lt;br /&gt;
! The increase in the number of Command Classes, clutters up the design. &lt;br /&gt;
! The increase in the number of objects, and all the algorithms use the same interface.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54277</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54277"/>
		<updated>2011-10-29T17:46:21Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated.&lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- &lt;br /&gt;
! Advantages&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- &lt;br /&gt;
! Disadvantages&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54276</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54276"/>
		<updated>2011-10-29T17:45:43Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated.&lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- Advantages&lt;br /&gt;
! &lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- Disadvantages&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54275</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54275"/>
		<updated>2011-10-29T17:45:25Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated.&lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- Advantages&lt;br /&gt;
! &lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|- Disadvantages&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54274</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54274"/>
		<updated>2011-10-29T17:44:43Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated.&lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54273</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54273"/>
		<updated>2011-10-29T17:43:50Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;designpatterns sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Comparison Factor&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Ensure only one object of a class is instantiated.&lt;br /&gt;
! &lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54272</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54272"/>
		<updated>2011-10-29T17:42:33Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;designpatterns sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Advantages&lt;br /&gt;
! Disadvantages&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54271</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54271"/>
		<updated>2011-10-29T17:41:18Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;designpatterns sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
! Adapter Pattern&lt;br /&gt;
! Command Pattern&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Advantages&lt;br /&gt;
! Disadvantages&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54270</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54270"/>
		<updated>2011-10-29T17:40:15Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Comparsion of the different Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;designpatterns sortable&amp;quot; style=&amp;quot;font-size: 90%; text-align: center; width: auto;&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
! Intent&lt;br /&gt;
! Advantages&lt;br /&gt;
! Drawbacks&lt;br /&gt;
|-&lt;br /&gt;
! Singleton Pattern&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
!&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54269</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54269"/>
		<updated>2011-10-29T17:37:24Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Design Patterns in a nutshell */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Comparsion of the different Design Patterns==&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54268</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54268"/>
		<updated>2011-10-29T17:36:39Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Differences between Command and Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==Design Patterns in a nutshell==&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54267</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54267"/>
		<updated>2011-10-29T17:36:06Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://www.oodesign.com/strategy-pattern.html Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54265</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54265"/>
		<updated>2011-10-29T17:35:27Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Adapter Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54263</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54263"/>
		<updated>2011-10-29T17:34:59Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com/command-pattern.html Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54261</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54261"/>
		<updated>2011-10-29T17:33:11Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54260</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54260"/>
		<updated>2011-10-29T17:30:17Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object, which is globally available. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54259</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=54259"/>
		<updated>2011-10-29T17:28:43Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns can be categorized and listed as below:&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Creational_pattern Creational Pattern], which help create the objects for the user, instead of having the user to instantiate the object.&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
[http://en.wikipedia.org/wiki/Structural_pattern Structural Pattern], which employ interfaces to achieve inheritance to enable objects to obtain new functionality.&lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
[http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral Pattern], which are concerned with communication between objects.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://www.oodesign.com/singleton-pattern.html Singleton patterns][http://en.wikipedia.org/wiki/Singleton_pattern (Wiki)]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?SingletonPattern Here ] is more to read on Singleton patterns.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://www.oodesign.com/adapter-pattern.html Adapter pattern][http://en.wikipedia.org/wiki/Adapter_pattern  (Wiki) ], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
 class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&amp;lt;br&amp;gt;&lt;br /&gt;
[http://c2.com/cgi/wiki?AdapterPattern This one ] is a good read on Adapter patterns and also contains links to other articles about Adapter Pattern.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://expertiza.csc.ncsu.edu/wiki/index.php/CSC/ECE_517_Fall_2011/ch3_3a_av#Closures Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
[http://c2c.com 3. More Object oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53675</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53675"/>
		<updated>2011-10-21T01:26:53Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;br /&gt;
&lt;br /&gt;
[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf 8. Lec 13 - Class Notes]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53507</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53507"/>
		<updated>2011-10-21T00:25:04Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53487</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53487"/>
		<updated>2011-10-21T00:20:12Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Object]. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53484</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53484"/>
		<updated>2011-10-21T00:19:47Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using [http://www.ruby-doc.org/core-1.9.2/Proc.html Proc Objects]. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53481</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53481"/>
		<updated>2011-10-21T00:19:05Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Closure_(computer_science) 7. Wiki - Closures]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53479</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53479"/>
		<updated>2011-10-21T00:18:32Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53477</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53477"/>
		<updated>2011-10-21T00:18:24Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
[http://en.wikipedia.org/wiki/Object-oriented_programming 2. Object - Oriented Programming]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53474</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53474"/>
		<updated>2011-10-21T00:17:50Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Design Patterns in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in [http://en.wikipedia.org/wiki/Object-oriented_programming Object - Oriented Languages] help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53471</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53471"/>
		<updated>2011-10-21T00:17:06Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Design Patterns in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_Patterns Design Pattern] is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53466</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53466"/>
		<updated>2011-10-21T00:15:21Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
[http://www.oodesign.com 1. Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53464</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53464"/>
		<updated>2011-10-21T00:14:14Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns - Elements of Reusable Object Oriented Software by Erich Gamma] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53462</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53462"/>
		<updated>2011-10-21T00:13:02Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
[http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53461</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53461"/>
		<updated>2011-10-21T00:12:53Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html 1. Article on Design Patterns in Ruby]&lt;br /&gt;
&lt;br /&gt;
[http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf 2. Design Patterns] &lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53415</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53415"/>
		<updated>2011-10-20T23:55:04Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Singleton_pattern 3. Wiki - Singleton Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Adapter_pattern 4. Wiki - Adapter Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern 5. Wiki - Command Pattern]&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Strategy_pattern 6. Wiki - Strategy Pattern]&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53411</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53411"/>
		<updated>2011-10-20T23:53:19Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Singleton_pattern&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Adapter_pattern&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Command_pattern&lt;br /&gt;
&lt;br /&gt;
http://en.wikipedia.org/wiki/Strategy_pattern&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53408</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53408"/>
		<updated>2011-10-20T23:51:40Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The [http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern] helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53405</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53405"/>
		<updated>2011-10-20T23:51:04Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command Patterns] lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53403</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53403"/>
		<updated>2011-10-20T23:50:05Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Closures and Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A [http://en.wikipedia.org/wiki/Closure_(computer_science) Closure] can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53400</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53400"/>
		<updated>2011-10-20T23:49:26Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Singleton Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the [http://en.wikipedia.org/wiki/Singleton_pattern Singleton pattern]. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A Closure can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this. &lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53397</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53397"/>
		<updated>2011-10-20T23:48:28Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A [http://www.oodesign.com/ Design Pattern] describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A Closure can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this. &lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53393</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53393"/>
		<updated>2011-10-20T23:47:05Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Design Patterns in Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - [http://www.patternlanguage.com/leveltwo/ca.htm Christopher Alexander]&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A Closure can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this. &lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53387</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53387"/>
		<updated>2011-10-20T23:44:46Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A Closure can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this. &lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53386</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53386"/>
		<updated>2011-10-20T23:44:27Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Command Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Closures and Patterns==&lt;br /&gt;
&lt;br /&gt;
Patterns can be implemented using Closures. A Closure can be described as a block of code ith the properties mentioned below:&lt;br /&gt;
&lt;br /&gt;
* The block of code can be passed around like a value.&lt;br /&gt;
* Any procedure or method that has the value can execute it.&lt;br /&gt;
* A Closure can refer to the variables from the context in which it was created. &lt;br /&gt;
&lt;br /&gt;
The Command Design Pattern and Strategy Design Patterns are examples of this. &lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53367</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53367"/>
		<updated>2011-10-20T23:37:13Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53366</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53366"/>
		<updated>2011-10-20T23:36:58Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Observer Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53365</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53365"/>
		<updated>2011-10-20T23:36:41Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x == y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
 sides.calArea(x,y)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53355</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53355"/>
		<updated>2011-10-20T23:20:29Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x = y&lt;br /&gt;
       puts &amp;quot;The given object is a square&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       puts &amp;quot;The given object is a rectangle&amp;quot;&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the values of x and y which are not known until runtime. Therefore, the algorithm that will be made use of to calculate the area, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53331</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53331"/>
		<updated>2011-10-20T23:08:16Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern discussed in class:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
Consider another example, which deals with areas of geometric shapes:&lt;br /&gt;
&lt;br /&gt;
 class findArea&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def calArea(x,y)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def area_square … end&lt;br /&gt;
     def area_rect … end&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
Here, we have a class findArea, which calculates the area of the given geometric shape by invoking the methods area_square or area_rect based upon whether the given figure is a square or rectangle.&lt;br /&gt;
&lt;br /&gt;
 sides = findArea.new&lt;br /&gt;
 if x = y&lt;br /&gt;
       sides.strategy = findArea.method(:area_square)&lt;br /&gt;
 else&lt;br /&gt;
       sides.strategy = findArea.method(:area_rect)&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53324</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53324"/>
		<updated>2011-10-20T22:57:17Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern discussed in class:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
Consider another example, which deals with different types of incoming data:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53310</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=53310"/>
		<updated>2011-10-20T22:37:45Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Algorithm Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The first design pattern one should know about is the Singleton pattern. It is basically the case, where the Class can instantiate only one object. &lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the general Syntax of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     include Singleton&lt;br /&gt;
     ...&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
It could be noticed that one must 'require' a module called 'singleton' and include the module within the class.&lt;br /&gt;
This adds the singleton flavour to your class. Now to test if it can create multiple instances, we can inquisitively try this,&lt;br /&gt;
&lt;br /&gt;
  raisesError = Example.new&lt;br /&gt;
  ruby-1.9.2-p290 :026 &amp;gt; raisesError = Example.new&lt;br /&gt;
  NoMethodError: private method `new' called for Example:Class&lt;br /&gt;
&lt;br /&gt;
We will soon find out we are greeted with the NoMethodError( Some kind of an error is expected ). So how do we use a singleton pattern? We create an instance of it using the keyword 'instance'&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt; &lt;br /&gt;
 b = Example.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Example:0x9361cb8&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The above illustrates how to create the instance of the Singleton class. It also illustrates another point. Even though two 'instances' have supposedly been created, both of them happen to contain reference to the same object. &lt;br /&gt;
This further proves that only one object can be created for a Singleton.&amp;lt;BR&amp;gt;&lt;br /&gt;
That was the Ruby's default way to create singletons. Singletons can be created manually without using the 'singleton' module.&lt;br /&gt;
&lt;br /&gt;
 class Single_ton&lt;br /&gt;
 def initialize&lt;br /&gt;
    puts &amp;quot;Initialised&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 @@instance = Single_ton.new&lt;br /&gt;
  &lt;br /&gt;
 def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
 def print_something&lt;br /&gt;
   puts &amp;quot;This prints something&amp;quot;&lt;br /&gt;
 end&lt;br /&gt;
   &lt;br /&gt;
 private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The above snippet is actually a singleton class. A cursory reading shall read the logic behind singleton-ing the class. An object to the same class has been created as a class variable. a &amp;quot;.instance&amp;quot; method is defined. One more thing to notice is that, &amp;quot;new&amp;quot; method is made 'private'. This makes sure that one cannot create anymore objects of the singleton.&lt;br /&gt;
&lt;br /&gt;
 Single_ton.instance&lt;br /&gt;
 =&amp;gt; #&amp;lt;Single_ton:0x94483d4&amp;gt; &lt;br /&gt;
 Single_ton.instance.print_something&lt;br /&gt;
 This prints something&lt;br /&gt;
&lt;br /&gt;
The above just shows that our class 'behaves' like a singleton. we have a '.instance' method defind and it works.&lt;br /&gt;
The method could be accessed by accessing the lone object inside the class.&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
In other words, when we want our new class to support a pre-existing feature, instead of modifying our new class, we can 'wrap' a similar feature in our new class to behave like the older class so that the pre-existing feature is supported.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Consider a software company located in United Kingdom. Lets assume they have these classes in their system.&lt;br /&gt;
&lt;br /&gt;
 class Bicycle&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     puts 'Iam riding a bicycle'&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
 &lt;br /&gt;
The following class has a method which calls the method 'rideBiCycle' in the passed argument. &lt;br /&gt;
 &lt;br /&gt;
 class Tester&lt;br /&gt;
   def displayRide(object)&lt;br /&gt;
     object.rideBiCycle&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
Now, If an American company buys this British company and tries to reuse the code. It has the following class already to its use.&lt;br /&gt;
&lt;br /&gt;
class Bike&lt;br /&gt;
   def rideBike&lt;br /&gt;
     puts 'I am riding a bike'&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
But, the problem is that, Bike doesn't have the function 'rideBiCycle', so it cannot use the Tester class directly. If that tester class sits in a very important part of the code, there is a problem. Now the way out is to create a 'adapter' which assimilates a 'bike' object. This adapter is actually a 'Bike' which can mimic itself like a 'Bicycle' by supporting the method 'rideBiCycle'.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Now to understand it. We will create a bunch of instances.&lt;br /&gt;
&lt;br /&gt;
 cycle = Bicycle.new&lt;br /&gt;
 cycle.rideBiCycle&lt;br /&gt;
 Iam riding a bicycle&lt;br /&gt;
&lt;br /&gt;
 test = Tester.new&lt;br /&gt;
 test.displayRide(cycle)&lt;br /&gt;
&lt;br /&gt;
 bike = Bike.new&lt;br /&gt;
 bike.rideBike&lt;br /&gt;
 I am riding a bike &lt;br /&gt;
&lt;br /&gt;
So far, the respective objects are working well when used in isolation. But when we have to do this,&lt;br /&gt;
&lt;br /&gt;
  test.displayRide(bike)&lt;br /&gt;
  NoMethodError: undefined method `rideBiCycle' for #&amp;lt;Bike:0x94b4494&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We encounter an error which is pretty obvious because rideBiCycle is not defined in Bike. Now let us define the adapter,&lt;br /&gt;
 &lt;br /&gt;
 class BikeAdapter&lt;br /&gt;
   def initialize(bike)&lt;br /&gt;
     @bikeObject = bike&lt;br /&gt;
     end&lt;br /&gt;
   def rideBiCycle&lt;br /&gt;
     @bikeObject.rideBike&lt;br /&gt;
   end                                                                                                                 &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we can try the failed method call like this,&lt;br /&gt;
&lt;br /&gt;
 adapter = BikeAdapter.new(bike)&lt;br /&gt;
 test.displayRide(adapter)&lt;br /&gt;
 Iam riding a bike&lt;br /&gt;
&lt;br /&gt;
Voila! We have succesfully 'adapted' Bike class to behave like 'Bicycle'.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
The design patterns we saw above deal with classes and work around the way classes and objects are manipulated, interpreted, created etc. Actually, the adapter pattern bases its necessity on the need for 'code reuse'. But, it can only implement code reuse at a class or method level. What if we need a more finer to do it.For example, Sometimes one might need to store bunch of code to be used by a peer or himself at a later point. This bunch of code is not associated with any class, or any method. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Command patterns lends itself to our use at this point. With command pattern it is possible to store a piece of code or a set of commands to be executed at a later stage. And of course, this is implemented with the use of procs in Ruby.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
This is how command pattern is usually implemented. We have a class which holds the commands as a proc object. And whenever it is to be called, 'call' is used to execute that proc.&lt;br /&gt;
&lt;br /&gt;
 class Command&lt;br /&gt;
   def initialize(input)&lt;br /&gt;
     @local_proc = input &lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
   def return_proc&lt;br /&gt;
    @local_proc&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Now we have a class that stores a command. Now let us create a 'command'. &lt;br /&gt;
&lt;br /&gt;
 myProc = proc{ puts 8 }&lt;br /&gt;
&lt;br /&gt;
Now we have a proc object that we want to use.&lt;br /&gt;
&lt;br /&gt;
 obj = Command.new(myProc)&lt;br /&gt;
 obj.return_proc.call&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
Then we create an object storing myProc inside it. Then we use the object to return the proc and use 'call' on it to execute it. One could also create an array of commands by simply creating an array of Command objects and storing the proc in them. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
One important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an example of the Strategy Pattern discussed in class:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52454</id>
		<title>CSC/ECE 517 Fall 2011/ch4 4h sv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2011/ch4_4h_sv&amp;diff=52454"/>
		<updated>2011-10-18T15:53:31Z</updated>

		<summary type="html">&lt;p&gt;Svellan: /* Observer Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Design Patterns in Ruby=&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice&amp;quot;'' - '''Christopher Alexander'''&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design&amp;quot;.''&lt;br /&gt;
&lt;br /&gt;
A Design Pattern is a template to solve a problem which can be used in many different situations. It names, abstracts and identifies the key aspects of a common design structure that makes it useful for creating a reusable object - oriented design. Design Patterns in Object - Oriented Languages help show the relationship between the different classes and objects in the program. Design Patterns are tools for building software.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Overview==&lt;br /&gt;
&lt;br /&gt;
A Design Pattern describes a problem which repeatedly occurs in our environment and also the core of the solution to that problem, in a way that this solution can be re-used many times, without ever doing it the same way twice.&lt;br /&gt;
&lt;br /&gt;
The description of a pattern consists of:&lt;br /&gt;
* '''''The Pattern name''''', describes the design pattern, its solutions and consequences.&lt;br /&gt;
* '''''The problem the pattern solves''''', describes when to apply the pattern.&lt;br /&gt;
* '''''The solution''''', describes the elements that make up the design, their relationships, responsibilities, and collaborations.&lt;br /&gt;
* '''''The consequences''''', describes the results of applying the pattern to the problem.&lt;br /&gt;
&lt;br /&gt;
The different types of design patterns are listed below:&lt;br /&gt;
&lt;br /&gt;
* '''''Singleton Design Pattern''''', which is used to have only one instance of a class. &lt;br /&gt;
* '''''Adapter Design Pattern''''', which enables classes with incompatible interfaces to work together.&lt;br /&gt;
* '''''Observer Design Pattern''''', which defines a one - to - many dependency among objects such that, the change in state of an object causes all its dependents to be notified and updated automatically.&lt;br /&gt;
* '''''Command Design Pattern''''', which enables to pass around the code that needs to be executed later.&lt;br /&gt;
* '''''Algorithm Strategy Pattern''''', which helps choose an algorithm to fulfill a task based on some &amp;quot;parameter&amp;quot; of the situation.&lt;br /&gt;
&lt;br /&gt;
==Singleton Pattern==&lt;br /&gt;
The Singleton design pattern is used to restrict the instantiation of a class to only one instance which is globally available. This is used in situations where a user needs an instance of the class to be available in various parts of the application. The Singleton pattern is available as a mixin in the Ruby library. Including it in the code makes the new method private and provides an instance method used to create or access the single instance.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the implementation of Singleton Design Pattern in Ruby:&lt;br /&gt;
&lt;br /&gt;
 require 'singleton'&lt;br /&gt;
 class Example&lt;br /&gt;
     attr_accessor :val&lt;br /&gt;
     include Singleton&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
In the above declaration of a class, including the Singleton module makes the class's new method private. To create an object of that class, the users call the instance method, which returns a singleton instance of the class.&lt;br /&gt;
 &lt;br /&gt;
 a = Example.instance&lt;br /&gt;
 b = Example.instance&lt;br /&gt;
&lt;br /&gt;
Here, the instances a and b of the class Example are essentially the same object. When a value of 007 is assigned to a, the value of b is also the same.&lt;br /&gt;
&lt;br /&gt;
 a.val = 007&lt;br /&gt;
 puts b.val&lt;br /&gt;
 =&amp;gt; 007&lt;br /&gt;
&lt;br /&gt;
The Singleton Pattern is useful in situations where serialization is desirable, example logging, communication and database access etc. In these cases, only a single instance of the class is created which accesses and modifies the methods of the class. This ensures safety and also make the single instance responsible for the class.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Singleton Design Pattern without using the Singleton library. Here, we can observe that the class ExampleLog's new method has been explicitly declared to be private.&lt;br /&gt;
&lt;br /&gt;
 class ExampleLog&lt;br /&gt;
  def initialize&lt;br /&gt;
    @log = File.open(&amp;quot;logging.txt&amp;quot;, &amp;quot;a&amp;quot;)&lt;br /&gt;
  end&lt;br /&gt;
   &lt;br /&gt;
  @@instance = ExampleLog.new&lt;br /&gt;
 &lt;br /&gt;
  def self.instance&lt;br /&gt;
    return @@instance&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def log(message)&lt;br /&gt;
    @log.puts(message)&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  private_class_method :new&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 ExampleLog.instance.log('This is to illustrate the Singleton Pattern')&lt;br /&gt;
&lt;br /&gt;
In this example, we can see that an instance of the class ExampleLog is created in the same class ExampleLog. This instance can be accessed with class method ExampleLog.instance when there is something that needs to be written to the log file &amp;quot;logging.txt&amp;quot; using the class method log. The initialize method consists of a log file opened for appending. The method &amp;quot;new&amp;quot; has been declared to be private which does not permit the user to create any more new instances of class ExampleLog. This is a Singleton Pattern which provides us with only one instance that is globally available.&lt;br /&gt;
&lt;br /&gt;
==Adapter Pattern==&lt;br /&gt;
&lt;br /&gt;
An [http://en.wikipedia.org/wiki/Adapter_pattern Adapter Design Pattern], also known as the Wrapper Pattern enables classes with incompatible interfaces to work together, by providing the users with its interface. This is achieved by:&lt;br /&gt;
&lt;br /&gt;
* “Wrapping” its own interface around the interface of a pre-existing class. &lt;br /&gt;
* Besides, it may also translate data formats from the caller to a form needed by the callee. Example: If the caller stores boolean value in terms of integers but the callee needs the values as 'true/false', the adapter pattern would extract the right value from the caller and pass it on to the callee. This ensures that the caller and callee can work together. &lt;br /&gt;
&lt;br /&gt;
The purpose of an adapter is “to convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Adapter Pattern:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Types of Adapter Patterns===&lt;br /&gt;
&lt;br /&gt;
Based on the structure, there are two types of Adapter Patterns:&lt;br /&gt;
&lt;br /&gt;
====Object Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
The Adapter Pattern has an instance of the classit &amp;quot;wraps&amp;quot; and makes calls to this wrapped object alone.&lt;br /&gt;
&lt;br /&gt;
====Class Adapter Pattern====&lt;br /&gt;
&lt;br /&gt;
Polymorphic Interfaces are used by this type of pattern, where the interface that is expected and the interface that is available are both inherited or implemented. This is used in situations where the class providing the services you need does not provide the interface required.&lt;br /&gt;
&lt;br /&gt;
==Observer Pattern==&lt;br /&gt;
&lt;br /&gt;
The Observer Pattern, also known as the Dependents or Publish - Subscribe Pattern, defines a one-to-many dependency between objects such that the change in state of an object causes the dependent objects to be notified and updated automatically. &lt;br /&gt;
&lt;br /&gt;
Partitioning of an application into a collection of co-operating classes, would require consistency to be maintained among these classes.&lt;br /&gt;
&lt;br /&gt;
==Command Pattern==&lt;br /&gt;
&lt;br /&gt;
At times, it is needed to pass around code that needs to be executed later. This is achieved by employing the Command Design Pattern which is used to factor out the action code of a class into its own object. It encapsulates a request as an object, allowing clients with different requests to be parameterized. The Command Pattern is implemented using Closures. A Command class holds an object, method along with the information needed to call a method at a later time. This information includes the method name with the values for the method parameters. The 'Call' method of Ruby brings the different parts together when needed.&lt;br /&gt;
&lt;br /&gt;
This is illustrated using the following example:&lt;br /&gt;
&lt;br /&gt;
One example is a check at a restaurant.&lt;br /&gt;
* The waiter/waitress takes an order from a customer and writes it on a check.&lt;br /&gt;
* The check is then queued for the cook, who prepares the food as requested by the customer.&lt;br /&gt;
* The check is later returned to the server, who uses it to bill the customer.&lt;br /&gt;
&lt;br /&gt;
In this example, the check has nothing to do with the menu. In principle, the same checks could be used at any restaurant.&lt;br /&gt;
&lt;br /&gt;
The command pattern is made up of a client, invoker and receiver. A client creates an object of the Command class and provides the information required to call the method at a later time. The invoker decides when the method should be called. The receiver is the class object which contains the code of the method. The usage of command objects makes it easier to construct components which need to execute methods at a later time without having knowledge about the method's owner or parameters.&lt;br /&gt;
&lt;br /&gt;
This Pattern can be implemented using the Proc objects, which is a callable block of code that closes over the variables in scope when it was created. This gives us a concise implementation of Command Pattern.&lt;br /&gt;
&lt;br /&gt;
Below is an illustration of the Command Design Pattern, using Proc Objects:&lt;br /&gt;
&lt;br /&gt;
 count = 0&lt;br /&gt;
 commands = []&lt;br /&gt;
 (1..10).each do |i|&lt;br /&gt;
 commands &amp;lt;&amp;lt; proc { count += i }&lt;br /&gt;
 end&lt;br /&gt;
 puts &amp;quot;Count is initially #{count}&amp;quot;&lt;br /&gt;
 commands.each { |cmd| cmd.call }&lt;br /&gt;
 puts &amp;quot;Performed all commands. count is #{count}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
Another important use of the Command Pattern is to enable the client undo what he has already done, or redo what has been undone by the user. This is done by maintaining a history of the commands executed. As and when the user makes changes, the system creates command after command, executing each command immediately to effect the change. Every undo - able command holds two methods - the execute and the unexecute method.&lt;br /&gt;
The commands are stored in a list and when the user decides to undo a change, the last command on the list is executed to undo the change. The changes can be undone, by going back the history of commands. The redo is done in the similar way where the the commands are re-executed beginning from the last change that what undone to reapply the changes undone. A simple example of the undo - redo use of a Command Pattern is a Calculator with many undo - redo options.&lt;br /&gt;
&lt;br /&gt;
==Algorithm Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
The Strategy Pattern helps choose an algorithm to accomplish a task based on some &amp;quot;parameters&amp;quot; of the situation. Also known as the Policy Pattern, it enables selection of algorithms at runtime. This pattern allows the algorithm to vary irresepctive of the user that uses it.&lt;br /&gt;
&lt;br /&gt;
The strategy pattern ''&amp;quot;defines a family of algorithms, encapsulates each one, and makes them interchangeable&amp;quot;''.&lt;br /&gt;
&lt;br /&gt;
Consider as an example, a class that that converts among different types of file formats like jpeg, jif, png etc. We can write a case statement to choose what algorithm has to be employed for each type of format. Another example could be performing validation on incoming data. Here we can select a validation algorithm based on the type and source of data.  &lt;br /&gt;
&lt;br /&gt;
A Strategy Pattern is best implemented using Proc Objects. Below is an illustration of it:&lt;br /&gt;
&lt;br /&gt;
 class RoutePlanner&lt;br /&gt;
     attr_accessor :strategy&lt;br /&gt;
     def go(pointA, pointB)&lt;br /&gt;
          strategy.call(*args)&lt;br /&gt;
     end&lt;br /&gt;
     def fastest … end&lt;br /&gt;
     def shortest … end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Here, we have a class which helps us plan the route between two points A and B, depending upon whether we want the shortest path or the fastest path.&lt;br /&gt;
&lt;br /&gt;
 ctx = RoutePlanner.new&lt;br /&gt;
 if on_foot&lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:shortest)&lt;br /&gt;
 else &lt;br /&gt;
    ctx.strategy = RoutePlanner.method(:fastest)&lt;br /&gt;
 ctx.go(pointA, pointB)&lt;br /&gt;
&lt;br /&gt;
Here, the path or strategy chosen is based on an if condition involving the value of the variable on_foot which is not known until runtime. Therefore, the algorithm that will be made use of to reach B starting from A, is decided at run time. The strategy that will be chosen depends on many factors which change dynamically.&lt;br /&gt;
&lt;br /&gt;
==Differences between Command and Strategy Pattern==&lt;br /&gt;
&lt;br /&gt;
* A Command Pattern encapsulates a single action. A command object has a single method with a generic signature associated with it. &lt;br /&gt;
* A Strategy Pattern, enables us to customize an algorithm, deciding which algorithm has to be made use of depending on a number of dynamically changing factors. A given strategy has many methods associated with it.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
http://designpatternsinruby.com/section01/article.html&lt;br /&gt;
http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf&lt;/div&gt;</summary>
		<author><name>Svellan</name></author>
	</entry>
</feed>