<?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=Nshetty2</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=Nshetty2"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Nshetty2"/>
	<updated>2026-05-09T19:09:18Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37699</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37699"/>
		<updated>2010-10-07T02:51:02Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic and Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy Pattern is to define a family of algorithms,[http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29 encapsulate] each algorithm, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining two separate modules named TextCompressor and ImageCompressor.  The appropriate module&lt;br /&gt;
would be included in the CompressContext. By invoking the module method we would be selecting appropriate strategy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern succinct with the use of blocks and other such language constructs.By using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. The block language construct of Ruby can be used to implement different behaviors at run time, in addition to being included in modules at compile time.This flexibility is allowed by meta-programming.Also,Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37697</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37697"/>
		<updated>2010-10-07T02:49:06Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic and Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy Pattern is to define a family of algorithms, encapsulate each algorithm, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining two separate modules named TextCompressor and ImageCompressor.  The appropriate module&lt;br /&gt;
would be included in the CompressContext. By invoking the module method we would be selecting appropriate strategy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern succinct with the use of blocks and other such language constructs.By using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. The block language construct of Ruby can be used to implement different behaviors at run time, in addition to being included in modules at compile time.This flexibility is allowed by meta-programming.Also,Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37694</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37694"/>
		<updated>2010-10-07T02:46:34Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Objective */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic and Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining two separate modules named TextCompressor and ImageCompressor.  The appropriate module&lt;br /&gt;
would be included in the CompressContext. By invoking the module method we would be selecting appropriate strategy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern succinct with the use of blocks and other such language constructs.By using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. The block language construct of Ruby can be used to implement different behaviors at run time, in addition to being included in modules at compile time.This flexibility is allowed by meta-programming.Also,Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37688</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37688"/>
		<updated>2010-10-07T02:39:22Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining two separate modules named TextCompressor and ImageCompressor.  The appropriate module&lt;br /&gt;
would be included in the CompressContext. By invoking the module method we would be selecting appropriate strategy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern succinct with the use of blocks and other such language constructs.By using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. The block language construct of Ruby can be used to implement different behaviors at run time, in addition to being included in modules at compile time.This flexibility is allowed by meta-programming.Also,Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37686</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37686"/>
		<updated>2010-10-07T02:38:33Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining two separate modules named TextCompressor and ImageCompressor.  The appropriate module&lt;br /&gt;
would be included in the in CompressContext. By invoking the module method we would be selecting appropriate strategy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Below is the Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern succinct with the use of blocks and other such language constructs.By using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. The block language construct of Ruby can be used to implement different behaviors at run time, in addition to being included in modules at compile time.This flexibility is allowed by meta-programming.Also,Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37674</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37674"/>
		<updated>2010-10-07T02:31:48Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider an example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks and other such language constructs.Using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go.Also Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37672</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37672"/>
		<updated>2010-10-07T02:27:42Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Components_in_the_implementation_of_a_strategy_pattern ConcreteStrategy] classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks and other such language constructs.Using its block construct,one can eliminate the behavioral classes altogether.  The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go.Also Ruby provides better code re-usability through the use of unbounded polymorphism.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37665</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37665"/>
		<updated>2010-10-07T02:24:11Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az#Implementation_of_Strategy_pattern_in_Static_Object_Oriented_language section 3.2], modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different ConcreteStrategy classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks and other such language constructs. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go.Also Ruby provides better code re-usability through the use of unbounded polymorphism.The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37663</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37663"/>
		<updated>2010-10-07T02:22:38Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation[[#References|&amp;lt;sup&amp;gt;8&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a strategy pattern==&lt;br /&gt;
The components in the implementation of a strategy pattern are[[#References|&amp;lt;sup&amp;gt;1&amp;lt;/sup&amp;gt;]]:&lt;br /&gt;
&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]] [[#References|&amp;lt;sup&amp;gt;7&amp;lt;/sup&amp;gt;]]-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy_pattern_az.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example section 3.2, modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different ConcreteStrategy classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks and other such language constructs. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go.Also Ruby provides better code re-usability through the use of unbounded polymorphism.The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 Class notes]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
#[http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Differences between Strategy and Bridge Patterns]&lt;br /&gt;
#[http://powerdream5.wordpress.com/2007/10/05/the-differences-between-strategy-pattern-and-state-pattern/ Differences between Strategy and State Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Strategy_pattern_az.jpg&amp;diff=37661</id>
		<title>File:Strategy pattern az.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Strategy_pattern_az.jpg&amp;diff=37661"/>
		<updated>2010-10-07T02:21:50Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37630</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37630"/>
		<updated>2010-10-07T02:00:45Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example section 3.2, modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different ConcreteStrategy classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 CLASS NOTES]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37624</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37624"/>
		<updated>2010-10-07T01:59:17Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern] in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example section 3.2, modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different ConcreteStrategy classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 CLASS NOTES]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern wiki]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37620</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37620"/>
		<updated>2010-10-07T01:57:55Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern] in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy pattern in a static object-oriented language like Java, we need to define a common interface and the interface must be extended by other classes which implement different algorithm.In the case of dynamic object-oriented language like Ruby,as we have seen in the example section 3.2, modules can be extended to include strategy methods.There is no need to have a common interface.&lt;br /&gt;
* In static object-oriented languages, to implement each algorithm we need to have different interface/strategy which would be extended by different ConcreteStrategy classes.This would result in increasing the code base.Whereas in dynamic object-oriented languages, each module can have different strategies and the context class can include the module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
#[http://courses.ncsu.edu/csc517//common/lectures/notes/lec13.pdf CSC517 CLASS NOTES]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37613</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37613"/>
		<updated>2010-10-07T01:55:22Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern] in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above implementation,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and the appropriate module&lt;br /&gt;
would be included in the context, i.e in CompressContext, the appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking this method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can :extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;br /&gt;
==See Also==&lt;br /&gt;
#http://wiki.asp.net/page.aspx/289/strategy/&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37606</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37606"/>
		<updated>2010-10-07T01:52:28Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
After reading this Wiki Chapter,the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# What is a [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern], when and how to use it.&lt;br /&gt;
# Implementation of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern] in Dynamic, Static object oriented languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps the user to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditional statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern is to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
'''''Strategy''''':   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
'''''ConcreteStrategy''''':&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
'''''Context''''':&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to the strategy pattern. Listed below are the key differences-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate the strategy pattern, let us consider the example of a basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files are compressed by using different compression algorithms.&lt;br /&gt;
Here we would  like to demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how these objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having an instance of IcompressStrategy. IcompressStrategy  is the common interface and it defines generic algorithmic methods which would be extended by various classes which implement the algorithm. Having instance of the interface in the Applicationobject would make it possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of the object-oriented principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can :extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37573</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37573"/>
		<updated>2010-10-07T01:41:46Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Objective */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az Strategy pattern], when and how to use strategy pattern.&lt;br /&gt;
# Implementation of [http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2010/ch3_3h_az strategy pattern] in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of OO principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37572</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37572"/>
		<updated>2010-10-07T01:40:39Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Objective */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of OO principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go. The dynamic nature of Ruby allows for better implementation of the Strategy Pattern.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37569</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37569"/>
		<updated>2010-10-07T01:39:44Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of OO principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 closure] feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
The dynamic feature of Ruby helps make implementation of Strategy Pattern in Ruby succinct and clear with the use of blocks. The static nature of Java requires it to create a interface and extend it to implement different behaviors. As a result the code is usually longer. Also Ruby allows for implementing different behaviors on the go.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37558</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37558"/>
		<updated>2010-10-07T01:35:00Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have [http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of OO principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37557</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37557"/>
		<updated>2010-10-07T01:33:58Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have[http://en.wikipedia.org/wiki/Factory_pattern Factory] method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37556</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37556"/>
		<updated>2010-10-07T01:32:32Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following [http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37554</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37554"/>
		<updated>2010-10-07T01:31:56Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following http://en.wikipedia.org/wiki/Unified_Modeling_Language UML] class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37553</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37553"/>
		<updated>2010-10-07T01:30:33Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Related Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Bridge_pattern Bridge] and [http://en.wikipedia.org/wiki/State_pattern State] pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37551</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37551"/>
		<updated>2010-10-07T01:28:34Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design[3].&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
Bridge and State pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37550</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37550"/>
		<updated>2010-10-07T01:27:26Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
Bridge and State pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and Dynamic object-oriented languages,the design pattern framework can be implemented with equal clarity. But the Java language does not use language idioms, which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Design Pattern Wiki]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37547</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37547"/>
		<updated>2010-10-07T01:26:40Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 design pattern] is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
Bridge and State pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
* In both the Static object-oriented and dynamic object-oriented languages,the design pattern framework can be implemented with clarity. The Java does not use language idioms which makes the code easier to understand if one knows object-oriented languages. In Ruby, using blocks and other such language specific constructs might make understanding the code a little more tough.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37542</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37542"/>
		<updated>2010-10-07T01:16:52Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Related Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
Bridge and State pattern are related patterns to Strategy. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| It defines a family of algorithms, encapsulate each one and make them interchangeable.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.The bridge pattern is useful when both the class as well as what it does vary often. The class itself can be thought of as the implementation and what the class can do as the abstraction.&lt;br /&gt;
| It allows an object to alert its behavior when its internal state changes. The object will appear to change its class.&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static object-oriented language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static object-oriented language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic object-oriented as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static object-oriented, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic object-oriented, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37511</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37511"/>
		<updated>2010-10-07T00:56:41Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated in objects and &lt;br /&gt;
how this objects can be selected without using the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static oo language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37507</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37507"/>
		<updated>2010-10-07T00:53:42Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static oo language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37505</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37505"/>
		<updated>2010-10-07T00:52:16Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==Related Patterns==&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static oo language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37503</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37503"/>
		<updated>2010-10-07T00:51:17Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Components in the implementation of a Strategy pattern: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern[1]:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static oo language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37497</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37497"/>
		<updated>2010-10-07T00:45:27Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Comparison of Dynamic and Static o-o language features in implementing Strategy pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy Pattern in Dynamic and Static oo language:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37496</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37496"/>
		<updated>2010-10-07T00:44:29Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure feature of Ruby language to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy inDynamic and Static oo llity anguage:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37493</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37493"/>
		<updated>2010-10-07T00:43:21Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Comparison of Dynamic and Static o-o language features in implementing Strategy pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure language feature of Ruby to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
The following are the key observations noted when we implemented Strategy inDynamic and Static oo llity anguage:&lt;br /&gt;
* For implementing the strategy in static, we need to define common interface and the interface must be extended by &lt;br /&gt;
:other class which implements different algorithm.In Case of Dynamic oo as we have seen in the example section 3.2, modules can extended to :include strategy methods.No need to have common interface.&lt;br /&gt;
* In Static o-o, to implement each algorithm we need to have different interface/Strategy which would be extended by different :ConcreteStrategy Class.This would result in increasing the code base.Where as in the dynamic oo, each module can have different strategy's :and context class can include module and selectively call the appropriate method.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37428</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37428"/>
		<updated>2010-10-07T00:10:01Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompressor&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompressor)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Here is quick overview of how we are implementing,&lt;br /&gt;
we are defining concreteStrategy in separate modules TextCompressor and ImageCompressor and appropriate module&lt;br /&gt;
would be included in context, i.e in CompressContext and appropriate strategy would be used by invoking the method&lt;br /&gt;
implemented in the module.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the above code, we are making use of closure language feature of Ruby to initialize appropriate strategy in initialize method &lt;br /&gt;
and invoking the method later.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37410</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37410"/>
		<updated>2010-10-07T00:00:06Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| The Bridge pattern deals with HOW you deal with multiple implementations. The Bridge pattern is meant for structure.&lt;br /&gt;
| The State pattern deals with HOW you switch between different states/implementations&lt;br /&gt;
|-&lt;br /&gt;
| One way to implement the Strategy pattern is to use the Bridge pattern approach or the State pattern approach, but the strategy pattern can also be &lt;br /&gt;
implemented a number of other ways.&lt;br /&gt;
| You cannot use the strategy pattern to implement bridge pattern.&lt;br /&gt;
| You cannot use the strategy pattern to implement state pattern.&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37399</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37399"/>
		<updated>2010-10-06T23:57:21Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
&lt;br /&gt;
The Strategy pattern is different form the Bridge and State pattern. Listed below are the differences and when  to use each of them-&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; cellspacing=&amp;quot;0&amp;quot; cellpadding=&amp;quot;5&amp;quot; align=&amp;quot;center&amp;quot;&lt;br /&gt;
! Strategy Pattern&lt;br /&gt;
! Bridge Pattern&lt;br /&gt;
! State Pattern&lt;br /&gt;
|- &lt;br /&gt;
| The Strategy pattern deals with USING different implementations to implement &lt;br /&gt;
different algorithms. Strategy pattern is meant for behavior.&lt;br /&gt;
| ^So&lt;br /&gt;
| match&lt;br /&gt;
|-&lt;br /&gt;
| &amp;quot;This is a line. \nSo is this&amp;quot;&lt;br /&gt;
| \ASo&lt;br /&gt;
| no match&lt;br /&gt;
|}&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Components in the implementation of a Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithms for different types of file. &lt;br /&gt;
Image files and text files can be compressed by different algorithms,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
# http://www.dofactory.com/Patterns/PatternStrategy.aspx&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37287</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37287"/>
		<updated>2010-10-06T23:00:54Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37284</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37284"/>
		<updated>2010-10-06T22:57:28Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Comparison of Dynamic and Static o-o language features in implementing Strategy pattern ==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37282</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37282"/>
		<updated>2010-10-06T22:55:11Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Static Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Java Code to demonstrate strategy Pattern implementation &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Main Method of Context Class..&lt;br /&gt;
&lt;br /&gt;
 File file = getFile(); //get file path..&lt;br /&gt;
 Compresser  ci = CompresserFactory.getCompresser( file.type() );&lt;br /&gt;
 ci.performAction();&lt;br /&gt;
&lt;br /&gt;
// implementations:&lt;br /&gt;
   interface  Compresser  {&lt;br /&gt;
        public void compressAction();&lt;br /&gt;
  } &lt;br /&gt;
&lt;br /&gt;
 class ImageCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction() {&lt;br /&gt;
             // Use Standard JPEG compress algorithm ....&lt;br /&gt;
         }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class TextCompressStrategy implements Compresser { &lt;br /&gt;
         public void compressAction () {&lt;br /&gt;
             // use Haufman text compress algorithm.&lt;br /&gt;
         }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37277</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37277"/>
		<updated>2010-10-06T22:52:50Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Text File&lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Compress Image File&lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37274</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37274"/>
		<updated>2010-10-06T22:51:22Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of blocks to illustrate strategy pattern&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(&amp;amp;strategy)&lt;br /&gt;
    @strategy = strategy&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def compress&lt;br /&gt;
    @strategy.call&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Doing the task the normal way &lt;br /&gt;
textCompresser = CompressContext.new { puts 'block which can be used for text compression' }&lt;br /&gt;
textCompresser.compress &lt;br /&gt;
&lt;br /&gt;
#=&amp;gt; Doing the task alternatively &lt;br /&gt;
imageCompresser = CompressContext.new { puts 'block which can be used for Image compression' }&lt;br /&gt;
imageCompresser.compress &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37268</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37268"/>
		<updated>2010-10-06T22:49:11Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Implementation of Strategy pattern in Dynamic Object Oriented language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
Ruby Code which makes use of o-o principle to illustrate strategy pattern&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
class CompressContext&lt;br /&gt;
  def initialize(compression_strategy)&lt;br /&gt;
    self.class.send :include, compression_strategy&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module TextCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the text file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
module ImageCompresser&lt;br /&gt;
  def compress&lt;br /&gt;
     puts 'Compression the image file'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
class Main&lt;br /&gt;
  def self.compressandSend(*args)&lt;br /&gt;
    file = args[0]&lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing text file&lt;br /&gt;
    compressType = CompressContext.new(TextCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
    &lt;br /&gt;
    #=&amp;gt; compressing image file&lt;br /&gt;
    compressType = CompressContext.new(ImageCompresser)&lt;br /&gt;
    compressType.compress    &lt;br /&gt;
 end&lt;br /&gt;
  &lt;br /&gt;
Main.compressandSend(&amp;quot;filepath&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37255</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37255"/>
		<updated>2010-10-06T22:43:54Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Dynamic Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
==Implementation of Strategy pattern in Static Object Oriented language==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37252</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37252"/>
		<updated>2010-10-06T22:41:53Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern in Static Object Oriented Language language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37251</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37251"/>
		<updated>2010-10-06T22:40:40Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
ApplicationObject  would be having instance of IcompressStrategy. IcompressStrategy  is the common interface and defines generic algorithmic method which would be extended by various classes which implements the algorithm. Having instance of interface in Applicationobject would make possible for selecting different algorithms interchangeably.&lt;br /&gt;
&lt;br /&gt;
Based on the  File type, we can instantiate the right kind of Compressor. Here we can also have Factory method which helps in deciding the right kind of subclass of IcompressStrategy to instantiate.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern in Static Object Oriented Language language=&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37250</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37250"/>
		<updated>2010-10-06T22:34:30Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
The following UML class diagram captures the scenario we are illustrating:&lt;br /&gt;
[[Image:strategy_pattern.jpg]]&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern in Static Object Oriented Language language=&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Strategy_pattern.jpg&amp;diff=37249</id>
		<title>File:Strategy pattern.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Strategy_pattern.jpg&amp;diff=37249"/>
		<updated>2010-10-06T22:32:33Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: The file contains the UML Diagram which explains the strategy pattern&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The file contains the UML Diagram which explains the strategy pattern&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37248</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37248"/>
		<updated>2010-10-06T22:23:50Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Generic Implementation and participant of Strategy pattern: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy:&lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context:&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern in Static Object Oriented Language language=&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37247</id>
		<title>CSC/ECE 517 Fall 2010/ch3 3h az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch3_3h_az&amp;diff=37247"/>
		<updated>2010-10-06T22:23:26Z</updated>

		<summary type="html">&lt;p&gt;Nshetty2: /* Illustration of Strategy pattern through real world example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Objective=&lt;br /&gt;
Objective of this Wiki Chapter is that the reader should be get an overview of&lt;br /&gt;
&lt;br /&gt;
# Strategy pattern, when and how to use strategy pattern.&lt;br /&gt;
# Implementation of strategy pattern in Dynamic, Static object oriented language by going through our example.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
A design pattern is a general reusable solution to a commonly occurring  problem in software design.&lt;br /&gt;
Strategy Pattern in one such pattern which helps to use multiple algorithms interchangeably to reduce    &lt;br /&gt;
multiple conditions statements. This helps in making the code more maintainable.&lt;br /&gt;
&lt;br /&gt;
The intent of Strategy pattern would be to define a family of algorithms, encapsulate each one, and &lt;br /&gt;
make them interchangeable.  Strategy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
As an algorithm evolves to handle more and more situations, it can become very complex and difficult &lt;br /&gt;
to maintain, Strategy pattern can be used to resolve this situation.&lt;br /&gt;
==When to use Strategy Pattern==&lt;br /&gt;
# Many related classes differ only in their behavior.&lt;br /&gt;
# You need different variants of an algorithm.  For example, defining different algorithms based on  space-time tradeoffs.&lt;br /&gt;
# A class defines many behaviors, and these appear as multiple conditional statements in its operations.Instead, move related conditional branches into their own Strategy class.&lt;br /&gt;
&lt;br /&gt;
==Generic Implementation and participant of Strategy pattern:==&lt;br /&gt;
Strategy:   &lt;br /&gt;
* Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.&lt;br /&gt;
ConcreteStrategy &lt;br /&gt;
* Implements the algorithm using the Strategy interface.&lt;br /&gt;
Context&lt;br /&gt;
* Is configured with a ConcreteStrategy object&lt;br /&gt;
* Maintains a reference to a Strategy object&lt;br /&gt;
* May define an interface that lets Strategy access its data.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern through real world example=&lt;br /&gt;
To illustrate strategy pattern let’s consider this example of basic file transfer scenario,&lt;br /&gt;
where we want to compress the file before transmission. &lt;br /&gt;
File compression can be achieved by different algorithm for different types of file. &lt;br /&gt;
Image files and text files can be compresses by different algorithm,&lt;br /&gt;
here we would  like demonstrate how this algorithm can be encapsulated and &lt;br /&gt;
helps in selecting the algorithms without the conditional statements.&lt;br /&gt;
&lt;br /&gt;
=Illustration of Strategy pattern in Static Object Oriented Language language=&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;/div&gt;</summary>
		<author><name>Nshetty2</name></author>
	</entry>
</feed>