<?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=Sbselvad</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=Sbselvad"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Sbselvad"/>
	<updated>2026-06-08T21:30:22Z</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_2007/wiki3_8_ss&amp;diff=9746</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9746"/>
		<updated>2007-11-20T03:53:32Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Bob Martin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
Object Oriented Programming (OOP) is a methodology for modelling and designing software systems which encompasses the basic concepts of encapsulation, abstraction, inheritance and polymorphism. This methodology is guided by a set of principles, called the Design Principles. These principles show the right direction of designing and helps in avoiding costly mistakes at the designing stage. The design principles are not important only from a software design point of view, but also from a business because design principles enable flexible designs, which can evolve with the business requirements with minimum effort, time and cost.&lt;br /&gt;
&lt;br /&gt;
There are Class Desgin principles and Package Design principles, here we will discuss on the Package Design principles alone.&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
[http://www.objectmentor.com/omTeam/martin_r.html Robert C. Martin] has been a software professional since 1970. In the last 35 years, he has worked in various capacities on literally hundreds of software projects. He has authored &amp;quot;landmark&amp;quot; books on Agile Programming, Extreme Programming, UML, Object-Oriented Programming, and C++ Programming. He has published dozens of articles in various trade journals. Today, He is one of the software industry's leading authorities on Agile software development and is a regular speaker at international conferences and trade shows. He is a former editor of the C++ Report.&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction Principle (SAP) =&lt;br /&gt;
&lt;br /&gt;
Stable Abstraction Principle (SAP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stable Abstractions Principle (SAP) can be considered to be a corollary of the Stable Dependencies Principle (SDP), which said that packages should depend only on more stable packages. Applying Dependency Inversion Principle (DIP), flexibility is built into a design by introducing abstract classes. Concrete classes depend on the abstract classes for reusability and extensibility. An abstract class tends to be more stable, as it does not depend on concrete classes. The concrete classes, which are instable, and are expected to change, depend on the concrete classes. This ultimately means, that dependencies in packages are in the direction of abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets try to explain this by discussion. An abstraction is used to contain high-level design, which is used to hold the concept. Objects of concepts don’t exist in nature, and rightly so abstract classes cannot be instantiated. The concrete classes which depend on the abstractions are instantiated and can be modelled after the real world objects. As time goes by, the real world objects change more frequently than the concept itself. This leads us to a corollary that abstraction brings stability.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
=== Books ===&lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Designing-Object-Oriented-Applications-Method/dp/0132038374 Designing Object-Oriented C++ Applications using the Booch Method]&lt;br /&gt;
# [http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445 Agile Software Development: Principles, Patterns and Practices]&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
# [http://labs.cs.utt.ro/labs/acs/html/lectures/6/lecture6.pdf Principles of Object-oriented design]&lt;br /&gt;
# [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf Presentation on package design principles]&lt;br /&gt;
# [http://tdoks.blogspot.com/2007/02/principles-of-software-development.html Principles of Software Development]&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
# [http://www.java-entrepreneur.com/50226711/design_patterns_versus_design_principles.php Design Patterns Vs Design Principles]&lt;br /&gt;
# [http://www.artima.com/forums/flat.jsp?forum=276&amp;amp;thread=134747 Discussion on Design Patterns Vs Design Principles]&lt;br /&gt;
# [http://www.xp.co.nz/Coding_Standards_for_Java.htm Exteme programming and Design Principles]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9745</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9745"/>
		<updated>2007-11-20T03:52:35Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
Object Oriented Programming (OOP) is a methodology for modelling and designing software systems which encompasses the basic concepts of encapsulation, abstraction, inheritance and polymorphism. This methodology is guided by a set of principles, called the Design Principles. These principles show the right direction of designing and helps in avoiding costly mistakes at the designing stage. The design principles are not important only from a software design point of view, but also from a business because design principles enable flexible designs, which can evolve with the business requirements with minimum effort, time and cost.&lt;br /&gt;
&lt;br /&gt;
There are Class Desgin principles and Package Design principles, here we will discuss on the Package Design principles alone.&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
Robert C. Martin has been a software professional since 1970. In the last 35 years, he has worked in various capacities on literally hundreds of software projects. He has authored &amp;quot;landmark&amp;quot; books on Agile Programming, Extreme Programming, UML, Object-Oriented Programming, and C++ Programming. He has published dozens of articles in various trade journals. Today, He is one of the software industry's leading authorities on Agile software development and is a regular speaker at international conferences and trade shows. He is a former editor of the C++ Report.&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction Principle (SAP) =&lt;br /&gt;
&lt;br /&gt;
Stable Abstraction Principle (SAP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stable Abstractions Principle (SAP) can be considered to be a corollary of the Stable Dependencies Principle (SDP), which said that packages should depend only on more stable packages. Applying Dependency Inversion Principle (DIP), flexibility is built into a design by introducing abstract classes. Concrete classes depend on the abstract classes for reusability and extensibility. An abstract class tends to be more stable, as it does not depend on concrete classes. The concrete classes, which are instable, and are expected to change, depend on the concrete classes. This ultimately means, that dependencies in packages are in the direction of abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets try to explain this by discussion. An abstraction is used to contain high-level design, which is used to hold the concept. Objects of concepts don’t exist in nature, and rightly so abstract classes cannot be instantiated. The concrete classes which depend on the abstractions are instantiated and can be modelled after the real world objects. As time goes by, the real world objects change more frequently than the concept itself. This leads us to a corollary that abstraction brings stability.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
=== Books ===&lt;br /&gt;
&lt;br /&gt;
# [http://www.amazon.com/Designing-Object-Oriented-Applications-Method/dp/0132038374 Designing Object-Oriented C++ Applications using the Booch Method]&lt;br /&gt;
# [http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445 Agile Software Development: Principles, Patterns and Practices]&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
# [http://labs.cs.utt.ro/labs/acs/html/lectures/6/lecture6.pdf Principles of Object-oriented design]&lt;br /&gt;
# [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf Presentation on package design principles]&lt;br /&gt;
# [http://tdoks.blogspot.com/2007/02/principles-of-software-development.html Principles of Software Development]&lt;br /&gt;
&lt;br /&gt;
=== See Also ===&lt;br /&gt;
# [http://www.java-entrepreneur.com/50226711/design_patterns_versus_design_principles.php Design Patterns Vs Design Principles]&lt;br /&gt;
# [http://www.artima.com/forums/flat.jsp?forum=276&amp;amp;thread=134747 Discussion on Design Patterns Vs Design Principles]&lt;br /&gt;
# [http://www.xp.co.nz/Coding_Standards_for_Java.htm Exteme programming and Design Principles]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9707</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9707"/>
		<updated>2007-11-20T02:28:18Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Design principles related to Packages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
Object Oriented Programming (OOP) is a methodology for modelling and designing software systems which encompasses the basic concepts of encapsulation, abstraction, inheritance and polymorphism. This methodology is guided by a set of principles, called the Design Principles. These principles show the right direction of designing and helps in avoiding costly mistakes at the designing stage. The design principles are not important only from a software design point of view, but also from a business because design principles enable flexible designs, which can evolve with the business requirements with minimum effort, time and cost.&lt;br /&gt;
&lt;br /&gt;
There are Class Desgin principles and Package Design principles, here we will discuss on the Package Design principles alone.&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
Robert C. Martin has been a software professional since 1970. In the last 35 years, he has worked in various capacities on literally hundreds of software projects. He has authored &amp;quot;landmark&amp;quot; books on Agile Programming, Extreme Programming, UML, Object-Oriented Programming, and C++ Programming. He has published dozens of articles in various trade journals. Today, He is one of the software industry's leading authorities on Agile software development and is a regular speaker at international conferences and trade shows. He is a former editor of the C++ Report.&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction Principle (SAP) =&lt;br /&gt;
&lt;br /&gt;
Stable Abstraction Principle (SAP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stable Abstractions Principle (SAP) can be considered to be a corollary of the Stable Dependencies Principle (SDP), which said that packages should depend only on more stable packages. Applying Dependency Inversion Principle (DIP), flexibility is built into a design by introducing abstract classes. Concrete classes depend on the abstract classes for reusability and extensibility. An abstract class tends to be more stable, as it does not depend on concrete classes. The concrete classes, which are instable, and are expected to change, depend on the concrete classes. This ultimately means, that dependencies in packages are in the direction of abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets try to explain this by discussion. An abstraction is used to contain high-level design, which is used to hold the concept. Objects of concepts don’t exist in nature, and rightly so abstract classes cannot be instantiated. The concrete classes which depend on the abstractions are instantiated and can be modelled after the real world objects. As time goes by, the real world objects change more frequently than the concept itself. This leads us to a corollary that abstraction brings stability.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9702</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9702"/>
		<updated>2007-11-20T02:25:39Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Bob Martin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
Robert C. Martin has been a software professional since 1970. In the last 35 years, he has worked in various capacities on literally hundreds of software projects. He has authored &amp;quot;landmark&amp;quot; books on Agile Programming, Extreme Programming, UML, Object-Oriented Programming, and C++ Programming. He has published dozens of articles in various trade journals. Today, He is one of the software industry's leading authorities on Agile software development and is a regular speaker at international conferences and trade shows. He is a former editor of the C++ Report and currently writes a monthly Craftsman column for Software Development magazine.&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction Principle (SAP) =&lt;br /&gt;
&lt;br /&gt;
Stable Abstraction Principle (SAP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stable Abstractions Principle (SAP) can be considered to be a corollary of the Stable Dependencies Principle (SDP), which said that packages should depend only on more stable packages. Applying Dependency Inversion Principle (DIP), flexibility is built into a design by introducing abstract classes. Concrete classes depend on the abstract classes for reusability and extensibility. An abstract class tends to be more stable, as it does not depend on concrete classes. The concrete classes, which are instable, and are expected to change, depend on the concrete classes. This ultimately means, that dependencies in packages are in the direction of abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets try to explain this by discussion. An abstraction is used to contain high-level design, which is used to hold the concept. Objects of concepts don’t exist in nature, and rightly so abstract classes cannot be instantiated. The concrete classes which depend on the abstractions are instantiated and can be modelled after the real world objects. As time goes by, the real world objects change more frequently than the concept itself. This leads us to a corollary that abstraction brings stability.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9699</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9699"/>
		<updated>2007-11-20T02:21:14Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Stable Abstraction principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction Principle (SAP) =&lt;br /&gt;
&lt;br /&gt;
Stable Abstraction Principle (SAP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;Packages that are maximally stable should be maximally abstract. Instable packages should be concrete. The abstraction of a package should be in proportion to its stability.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Stable Abstractions Principle (SAP) can be considered to be a corollary of the Stable Dependencies Principle (SDP), which said that packages should depend only on more stable packages. Applying Dependency Inversion Principle (DIP), flexibility is built into a design by introducing abstract classes. Concrete classes depend on the abstract classes for reusability and extensibility. An abstract class tends to be more stable, as it does not depend on concrete classes. The concrete classes, which are instable, and are expected to change, depend on the concrete classes. This ultimately means, that dependencies in packages are in the direction of abstraction.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets try to explain this by discussion. An abstraction is used to contain high-level design, which is used to hold the concept. Objects of concepts don’t exist in nature, and rightly so abstract classes cannot be instantiated. The concrete classes which depend on the abstractions are instantiated and can be modelled after the real world objects. As time goes by, the real world objects change more frequently than the concept itself. This leads us to a corollary that abstraction brings stability.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9686</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9686"/>
		<updated>2007-11-20T02:11:41Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Stable Dependencies principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies Principle (SDP) = &lt;br /&gt;
&lt;br /&gt;
The Stable Dependencies Principle (SDP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependencies between packages in a design should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable that it is.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this principle, a package can be considered stable if it is not changed frequently, or if there is no need to change it frequently. Therefore, packages can reliably depend on this stable package. This principle can go to an atomic level of classes and say that classes should depend on more stable classes only. This is a direct derivation of the Dependency Inversion Principle (DIP). Why do we need to look at stability point of view? We need to do this as we define a dependency chain, which can help in localising the changes and create a system which is still flexible but not prone to erroneous changes.&lt;br /&gt;
&lt;br /&gt;
Quiet often, a single change in a class and hence a package triggers a chain-reaction causing changes to more than expected places. This is a classic case leading to overflow of estimates and sometimes leading to failure. By applying SDP the typical chain reaction can be avoided, as the more stable packages are lower in the chain.It is important to identify stable packages and unstable packages. The packages that are expected to changes should be designed to change and acknowledged accordingly. This information can give a better handle on the changes to be done and the corresponding estimates.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
An interface called &amp;lt;code&amp;gt;Logger&amp;lt;/code&amp;gt; will be more stable than classes &amp;lt;code&amp;gt;FileLogger&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;DBLogger&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9678</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9678"/>
		<updated>2007-11-20T02:02:12Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency Principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9676</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9676"/>
		<updated>2007-11-20T02:01:36Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency Principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency Principle (REP) states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9674</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9674"/>
		<updated>2007-11-20T01:59:31Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Acyclic Dependencies principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies Principle (ADP) =&lt;br /&gt;
&lt;br /&gt;
The Acyclic Dependency Priniciple (ADP) states that &lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The dependency structure between packages must be a Directed Acyclic Graph (DAG).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle is important from a deployment perspective. A package apart from being reusable and maintanable, should also be deployable. The package design should have defined dependencies so that it is deployment-friendly.&lt;br /&gt;
&lt;br /&gt;
DAG implies that in the package dependency traversal, there should be no path which includes the same package more than once. DAG leads to a clear dependency structure, which can instantly identify the packages that can get affected if a certain package is modified. Cyclic dependencies usually happen when multiple developers work on the same package, on the same module or on the same class. These cyclick dependencies can be broken. One of the methods is to use Dependency Inversion Principle (DIP).The dependency graphs lead directly to the map for building the packages and the entire application. Considering this at the early stages of package design saves lot of time and effort.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
The simplest cyclic dependency means that a package A depends on package B, which again depends on package A. Not only does this lead to wrong class dependencies, but also makes deployment a difficult and unmaintainable.&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9661</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9661"/>
		<updated>2007-11-20T01:49:28Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Common Reuse principle (CRP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse Principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9660</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9660"/>
		<updated>2007-11-20T01:49:17Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Common Closure principle (CCP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure Principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9659</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9659"/>
		<updated>2007-11-20T01:49:02Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency Principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9658</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9658"/>
		<updated>2007-11-20T01:48:48Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Common Reuse principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle (CRP) =&lt;br /&gt;
&lt;br /&gt;
The Common Reuse Principle (CRP) states that,&lt;br /&gt;
&lt;br /&gt;
    &amp;lt;code&amp;gt;The classes in a package are reused together. If you reuse one of the classes in a package, you reuse them all.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle stresses on the importance of a package (a set of cohesive classes) to be used for releasing reusable software, and the importance of releasing software that can be tracked, so that the users get a guarantee of interface and behaviour from the author.However, it is very important to decide which classes should be bound together by a package. Improper selection of classes can create unwanted dependencies and complicate usage of that package. Common Reuse Principle (CRP) lays down the foundations to determine which classes should be packaged together. &lt;br /&gt;
&lt;br /&gt;
According to this principle, only cohesive classes should be packaged together. The packaging of classes and the cohesiveness should be defined from the user’s perspective. If a user uses a package, all the classes in that package should be reusable in the same context. A rule that can be a derivative of this is that all the classes related to a functionality should be packaged together. For example, &lt;br /&gt;
&lt;br /&gt;
As specified by Reuse/Release Equivalency Principle (REP), even if one or two classes are being used, the granularity to be considered is the entire package. This dependency means that with every revision or release of that package, the software has to be tested and revalidated, irrespective of whether the modified class is being used or not. Hence, only those classes should be packaged together which will be reused together.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
For a matrix calculations pakage, it better have only those classes related to matrices and not trigonometric functions.&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9656</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9656"/>
		<updated>2007-11-20T01:40:39Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Common Closure principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle (CCP) =&lt;br /&gt;
&lt;br /&gt;
The Common Closure Principle (CCP) states that&lt;br /&gt;
&lt;br /&gt;
   &amp;lt;code&amp;gt;The classes in a package should be closed together against the same kind of changes. A change that affects a package affects all the classes in that package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This principle focuses at maintainability rather than usability. The principle guides on which classes should be packaged together from a change and distribution point of view. Though most of the class design principles advocate loose coupling between classes, so that they can change without affecting the other, dependencies between classes is inevitable. This principle tries to look at classes that are dependent on each other from a code change perspective. Even though Open-Closed Principle (OCP) recomments full closure to code change, it is not always possible. However this violation should be a conscious decision and the product should be ready for changes because of it.&lt;br /&gt;
&lt;br /&gt;
To enable easy distribution/update/release and maintainability, it is advisable to localise the changes to a package. Only the modified package can then be released, making the update easier. Even if the changes cannot be restricted to a single package, minimal packages should be required to be changed. To get an idea of the code change dependencies, we can take guidance from Law Of Demeter (LOD) principle, which tries to point out classes which know each other’s structures.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
A good example for application of CCP is a package modeled after a relational database. Class designs might need to be consciously violated during denormalization of the database model for performance improvements. During this time changes to the package affects all classes in that package.&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9646</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9646"/>
		<updated>2007-11-20T01:24:03Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this, package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9643</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9643"/>
		<updated>2007-11-20T01:22:32Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
     &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use Classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9640</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9640"/>
		<updated>2007-11-20T01:20:48Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &amp;lt;code&amp;gt;&amp;quot;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;quot;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use Classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9639</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9639"/>
		<updated>2007-11-20T01:20:31Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle (REP) */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use Classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9634</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9634"/>
		<updated>2007-11-20T01:20:02Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Reuse/Release Equivalency principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle (REP)=&lt;br /&gt;
&lt;br /&gt;
The Reuse/Release Equivalency principle states that &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;The granule of reuse is the granule of release. Only components that are released through a tracking system can be effectively reused. This granule is the package.&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
According to this package is to be considered as the unit of distribution of software. When one wants to reuse components, package has to be the concrete unit that should be reused. The term package may also be interpreted as library, domain, functionality or a set of classes bound together.&lt;br /&gt;
&lt;br /&gt;
An interesting argument can be that, why not use Classes as the unit of distribution? The reason is that quiet often by design, we end up with more than one class that communicate with each other. If individual classes are released, tracking version and compatibility can become very difficult. Hence, packages which are in the level next to classes, are the granular units of release and therfore reused even if classes can be the units of design.&lt;br /&gt;
&lt;br /&gt;
The tracking system is important because the author owns the responsibility of upgrading the package that is being reused. Changes or enhancements in the package can be triggered by many factors, like new standards, new requirements, bugs or sometimes new methodologies and technologies. The author has to track the changes in the package along with maintaining its source code.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
Lets say one wants use a file operations library. The library itself is the unit of distribution and usage (referred to as the package), even if we use only one particular class for reading a file.&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9618</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=9618"/>
		<updated>2007-11-20T01:08:10Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;br /&gt;
&lt;br /&gt;
= Design principles related to Packages =&lt;br /&gt;
&lt;br /&gt;
== Bob Martin ==&lt;br /&gt;
&lt;br /&gt;
= Reuse/Release Equivalency principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Closure principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Common Reuse principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Acyclic Dependencies principle =&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
= Stable Dependencies principle = &lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= Stable Abstraction principle =&lt;br /&gt;
&lt;br /&gt;
== Example == &lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
== Books ==&lt;br /&gt;
&lt;br /&gt;
== Links ==&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=8858</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 8 ss</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_8_ss&amp;diff=8858"/>
		<updated>2007-11-18T02:36:44Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Topic'''&lt;br /&gt;
&lt;br /&gt;
O-o design guru Bob Martin has developed a set of design principles that relate to packages (one level above the class-oriented strategies we consider in this course). Report on the&lt;br /&gt;
&lt;br /&gt;
    * Reuse/Release Equivalency principle,&lt;br /&gt;
    * Common Closure principle,&lt;br /&gt;
    * Common Reuse principle,&lt;br /&gt;
    * Acyclic Dependencies principle,&lt;br /&gt;
    * Stable Dependencies principle, and&lt;br /&gt;
    * Stable Abstraction principle&lt;br /&gt;
&lt;br /&gt;
Find the best descriptions and examples of each that you can find on the Web.&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5483</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5483"/>
		<updated>2007-10-11T00:50:08Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of [http://en.wikipedia.org/wiki/Algorithm algorithms], encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/[http://en.wikipedia.org/wiki/Mixins mixins] of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5482</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5482"/>
		<updated>2007-10-11T00:49:56Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Strategy Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of [[http://en.wikipedia.org/wiki/Algorithm algorithms], encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/[http://en.wikipedia.org/wiki/Mixins mixins] of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5481</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5481"/>
		<updated>2007-10-11T00:49:27Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Merits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/[http://en.wikipedia.org/wiki/Mixins mixins] of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5480</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5480"/>
		<updated>2007-10-11T00:48:46Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Where Ruby wins? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of [http://en.wikipedia.org/wiki/Algorithm algorithms] can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/[http://en.wikipedia.org/wiki/Mixins mixins] of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5479</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5479"/>
		<updated>2007-10-11T00:48:09Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of [http://en.wikipedia.org/wiki/Algorithm algorithms] can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism]/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5478</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5478"/>
		<updated>2007-10-11T00:47:02Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of [http://en.wikipedia.org/wiki/Algorithm algorithms] can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
[http://en.wikipedia.org/wiki/Meta_programming meta programming]&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic [http://en.wikipedia.org/wiki/Regular_expression regular expression] support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5477</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5477"/>
		<updated>2007-10-11T00:44:24Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Merits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of [http://en.wikipedia.org/wiki/Algorithm algorithms] can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5476</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5476"/>
		<updated>2007-10-11T00:43:38Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Structure */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like [http://en.wikipedia.org/wiki/Quicksort QuickSort], [http://en.wikipedia.org/wiki/Merge_sort MergeSort], [http://en.wikipedia.org/wiki/Bubble_sort Bubble Sort] etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5472</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5472"/>
		<updated>2007-10-11T00:40:31Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in [http://en.wikipedia.org/wiki/Object-oriented Object-oriented] designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5470</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5470"/>
		<updated>2007-10-11T00:39:52Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A [http://en.wikipedia.org/wiki/Design_pattern_(computer_science) design pattern] is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5469</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5469"/>
		<updated>2007-10-11T00:38:55Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Java's Solution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://java.sun.com/ Java] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5468</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5468"/>
		<updated>2007-10-11T00:37:56Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Ruby's Solution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the [http://www.ruby-lang.org/en/ Ruby] implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5467</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5467"/>
		<updated>2007-10-11T00:34:58Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Demerits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
Note: Most of these demerits are specific to statically typed languages like Java. Use of dynamic object oriented languages like Ruby can help bypass these demerits&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5465</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5465"/>
		<updated>2007-10-11T00:29:48Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Demerits */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= The Strategy Design Pattern, comparing a dynamic OO language to a static OO languaje =&lt;br /&gt;
&lt;br /&gt;
In this page we explore the differences in implementation and design of the Strategy design pattern between the dynamic Object-Oriented language Ruby and the static Object-Oriented language Java.&lt;br /&gt;
&lt;br /&gt;
= Definitions =&lt;br /&gt;
&lt;br /&gt;
== Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
== Strategy Pattern ==&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. &lt;br /&gt;
&lt;br /&gt;
The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application.&lt;br /&gt;
&lt;br /&gt;
=== Structure ===&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in a strategy pattern implementation&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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
=== Merits ===&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm are completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
=== Demerits ===&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
=== Related Patterns ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
=== Alternatives === &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Real World Case Study =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object instance containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object as an instance of a class implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface. The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt;. Therefore, &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; is the context class, the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface is the strategy, and &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; are concrete strategies.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5011</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5011"/>
		<updated>2007-10-01T16:16:43Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) edited this page.''&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4946</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4946"/>
		<updated>2007-10-01T05:59:05Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Where Ruby wins? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
&lt;br /&gt;
The comparison yields interesting insights into the advantages of Ruby over Java. Clearly Ruby has much higher edge in using OO design patterns like strategy over any other statically typed language.&lt;br /&gt;
&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4945</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4945"/>
		<updated>2007-10-01T05:54:22Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Java versus Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Unbounded polymorphism/mixins of Ruby provides better code reusability&lt;br /&gt;
# Highly dynamic strategies can be constructed using meta programming features of Ruby&lt;br /&gt;
# New algorithms can be evaluated dynamically with ease&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4941</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4941"/>
		<updated>2007-10-01T05:47:38Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the &lt;br /&gt;
algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using &lt;br /&gt;
meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using &amp;lt;code&amp;gt;eval&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Syntactic regular expression support not available.&lt;br /&gt;
| Interesting algorithm use cases can be constructed using the&lt;br /&gt;
syntactic regular expression support for Ruby&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4940</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4940"/>
		<updated>2007-10-01T05:41:51Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Algorithms for strategies need to be defined at compile time&lt;br /&gt;
| New algorithms for strategies can be created on the fly using meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism/Mixins makes one algorithm reusable &lt;br /&gt;
under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to evaluate new algorithms inside strategies &lt;br /&gt;
| New algorithms can be evaluated dynamically using eval&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| &lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4939</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4939"/>
		<updated>2007-10-01T05:29:15Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Strategies need to be defined at compile time&lt;br /&gt;
| New strategies can be created on the fly using meta programming&lt;br /&gt;
|-&lt;br /&gt;
| Difficult to include one algorithm under several strategies &lt;br /&gt;
| Unbounded polymorphism makes one algorithm reusable under several strategies&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4938</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4938"/>
		<updated>2007-10-01T05:25:18Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
The Strategy design pattern allows to define a family of algolrithms and makes them interchangeable. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone. The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by a &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
&lt;br /&gt;
// This is the Strategy interface&lt;br /&gt;
interface FileOperation {&lt;br /&gt;
  public void Run(String from, String to);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// These are the three different implementations&lt;br /&gt;
class CopyOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Copy &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class ReencodeOperation implements FileOperation {&lt;br /&gt;
  private int bitRate_; // In kbps&lt;br /&gt;
&lt;br /&gt;
  ReencodeOperation(int bitRate) {&lt;br /&gt;
    bitRate_ = bitRate;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Reencode &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot; at &amp;quot; + bitRate_ + &amp;quot;kbps.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class RenameOperation implements FileOperation {&lt;br /&gt;
  public void Run(String from, String to) {&lt;br /&gt;
    System.out.println(&amp;quot;Rename &amp;quot; + from + &amp;quot; to &amp;quot; + to + &amp;quot;.&amp;quot;);&lt;br /&gt;
  } &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// This is the context class&lt;br /&gt;
class FileJob {&lt;br /&gt;
  String from_;&lt;br /&gt;
  String to_;&lt;br /&gt;
  FileOperation op_;&lt;br /&gt;
&lt;br /&gt;
  public FileJob(String from, String to, FileOperation op) {&lt;br /&gt;
    from_ = from;&lt;br /&gt;
    to_ = to;&lt;br /&gt;
    op_ = op;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  void Run() {&lt;br /&gt;
    op_.Run(from_, to_);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Sample code&lt;br /&gt;
class Test {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    ArrayList&amp;lt;FileJob&amp;gt; jobs = new ArrayList&amp;lt;FileJob&amp;gt;();&lt;br /&gt;
&lt;br /&gt;
    FileOperation copyOp = new CopyOperation();&lt;br /&gt;
    FileOperation renameOp = new RenameOperation();&lt;br /&gt;
    FileOperation reencodeOp = new ReencodeOperation(128); // kbps&lt;br /&gt;
      &lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp));&lt;br /&gt;
    jobs.add(new FileJob(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp));&lt;br /&gt;
&lt;br /&gt;
    for (FileJob j : jobs) {&lt;br /&gt;
      j.Run();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# To give the file operation invocation a better name we define an alias, note&lt;br /&gt;
# that this is optional, one can just use 'call', but it is better to adapt&lt;br /&gt;
# the name to the problem domain&lt;br /&gt;
class FileOperation &amp;lt; Proc&lt;br /&gt;
  alias_method :run, :call&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# These are the strategies&lt;br /&gt;
copyOp     = FileOperation.new { |from, to| puts &amp;quot;Copy #{from} to #{to}.&amp;quot; }&lt;br /&gt;
renameOp   = FileOperation.new { |from, to| puts &amp;quot;Rename #{from} to #{to}.&amp;quot; }&lt;br /&gt;
bitRate = 128 # kbps&lt;br /&gt;
reencodeOp = FileOperation.new { |from, to| puts &amp;quot;Reencode #{from} to #{to} at #{bitRate}kbps.&amp;quot; }&lt;br /&gt;
&lt;br /&gt;
# This is the context class&lt;br /&gt;
class FileJob&lt;br /&gt;
  def initialize(from, to, op)&lt;br /&gt;
    @from = from&lt;br /&gt;
    @to = to&lt;br /&gt;
    @op = op&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def run&lt;br /&gt;
    @op.run(@from, @to)&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
# The same test as the Java implementation&lt;br /&gt;
jobs = Array.new&lt;br /&gt;
&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file1&amp;quot;, &amp;quot;file2&amp;quot;, copyOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file3&amp;quot;, &amp;quot;file4&amp;quot;, renameOp);&lt;br /&gt;
jobs &amp;lt;&amp;lt; FileJob.new(&amp;quot;file5&amp;quot;, &amp;quot;file6&amp;quot;, reencodeOp);&lt;br /&gt;
&lt;br /&gt;
jobs.each { |j| j.run() }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| An interface declaration, defines a common interface to the algorithm family&lt;br /&gt;
| No interface declaration required&lt;br /&gt;
|-&lt;br /&gt;
| A class is required for each strategy implementation&lt;br /&gt;
| No classes required, closures are good enough&lt;br /&gt;
|-&lt;br /&gt;
| Strategies need to be defined at compile time&lt;br /&gt;
| New strategies can be created on the fly using metaprogramming&lt;br /&gt;
|-&lt;br /&gt;
| This is not there&lt;br /&gt;
| This is there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby's code is succint, Java requires more boilerplate code due to the static nature of the language&lt;br /&gt;
# Closures make it trivial to add parameters to the stategy implementations, in Java parameters to strategies must be instance or class variables&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4776</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4776"/>
		<updated>2007-09-29T01:37:59Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Comparison */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented JAVA code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented RUBY code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation &lt;br /&gt;
! Ruby's Strategy Implementation&lt;br /&gt;
|- &lt;br /&gt;
| This is not there &lt;br /&gt;
| This is there&lt;br /&gt;
|-&lt;br /&gt;
| This is not there&lt;br /&gt;
| This is there&lt;br /&gt;
|-&lt;br /&gt;
| This is not there&lt;br /&gt;
| This is there&lt;br /&gt;
|-&lt;br /&gt;
| This is not there&lt;br /&gt;
| This is there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
| This is there&lt;br /&gt;
| This is not there&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby wins 1&lt;br /&gt;
# Ruby wins 2&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4775</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4775"/>
		<updated>2007-09-29T01:28:18Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented JAVA code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented RUBY code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparison ==&lt;br /&gt;
Let us perform a comparison to better understand the implementation&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;
! Java's Strategy Implementation ! Ruby's Strategy Implementation&lt;br /&gt;
|- | This is not there | This is there|-|&lt;br /&gt;
|- | This is not there| This is there|-|&lt;br /&gt;
|- | This is not there| This is there|-|&lt;br /&gt;
|- | This is not there| This is there|-|&lt;br /&gt;
|- | This is there| This is not there|-|&lt;br /&gt;
|- | This is there| This is not there|-|&lt;br /&gt;
|- | This is there| This is not there|-|&lt;br /&gt;
|- | This is there| This is not there|-|&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
# Ruby wins 1&lt;br /&gt;
# Ruby wins 2&lt;br /&gt;
# Ruby wins 3&lt;br /&gt;
# Ruby wins 4&lt;br /&gt;
# Ruby wins 5&lt;br /&gt;
# Ruby wins 6&lt;br /&gt;
# Ruby wins 7&lt;br /&gt;
# Ruby wins 8&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Playlist-Example.jpg&amp;diff=4774</id>
		<title>File:Playlist-Example.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Playlist-Example.jpg&amp;diff=4774"/>
		<updated>2007-09-29T01:21:30Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4773</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4773"/>
		<updated>2007-09-29T01:19:39Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Real World Problem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
[[Image:Playlist-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Java implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented JAVA code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
Here is the Ruby implementation of the playlist synchronization application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
Agustin.. Please paste your commented RUBY code here...&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparision ==&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4772</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4772"/>
		<updated>2007-09-29T01:14:23Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Example &amp;amp; Illustration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
[[Image:Strategy-Example.jpg||Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparision ==&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4771</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4771"/>
		<updated>2007-09-29T01:10:57Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Example &amp;amp; Illustration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg|Sort using Strategy]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparision ==&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4767</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4767"/>
		<updated>2007-09-29T01:03:31Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Real World Problem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparision ==&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4766</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=4766"/>
		<updated>2007-09-29T01:02:18Z</updated>

		<summary type="html">&lt;p&gt;Sbselvad: /* Real World Problem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: Santthosh (sbselvad@ncsu.edu) and Agustin (agusvega@nc.rr.com) are editing this page.&lt;br /&gt;
&lt;br /&gt;
= Strategy Pattern =&lt;br /&gt;
&lt;br /&gt;
Strategy pattern is one of the several software design patterns, where in algorithms can be selected on-the-fly at runtime execution of the program. The Strategy pattern lets one build software as a loosely coupled collection of interchangeable parts, in contrast to a monolithic, tightly coupled system. That loose coupling makes the software much more extensible, maintainable, and reusable. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. &lt;br /&gt;
&lt;br /&gt;
== Concept to explore ==&lt;br /&gt;
&lt;br /&gt;
''Take a case of the Strategy pattern and implement it as succinctly as possible in Ruby and Java. Compare the two implementations in terms of clarity and succinctness. The example should be a &amp;quot;real-world&amp;quot; example. While it may be grossly oversimplified for the purpose of illustration, it should not be totally contrived (i.e., should not raise the question, Why would anyone ever want to do that?).''&lt;br /&gt;
&lt;br /&gt;
== Definitions ==&lt;br /&gt;
&lt;br /&gt;
=== Design Patterns ===&lt;br /&gt;
&lt;br /&gt;
A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is neither a finished design nor a solution that can be transformed directly into code. It is a template on how to solve a problem that can be used in many different situations. Design patterns are more often use in Object oriented designs as they typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. &lt;br /&gt;
&lt;br /&gt;
Note: Algorithms are not design patterns, algorithms solve computational problems and design patterns solve design problems.&lt;br /&gt;
&lt;br /&gt;
=== Strategy Pattern ===&lt;br /&gt;
The Strategy Design Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeagle. Stratergy lets the algorithm vary independently from clients that use it.&lt;br /&gt;
&lt;br /&gt;
== Merits ==&lt;br /&gt;
&lt;br /&gt;
# A family of algorithms can be defined as a class hierarchy and can be used interchangeably to alter application behavior without changing its architecture.&lt;br /&gt;
# By encapsulating the algorithm separately, new algorithms complying with the same interface can be easily introduced.&lt;br /&gt;
# The application can switch strategies at run-time.&lt;br /&gt;
# Strategy enables the clients to choose the required algorithm, without using a &amp;quot;switch&amp;quot; statement or a series of &amp;quot;if-else&amp;quot; statements.&lt;br /&gt;
# Data structures used for implementing the algorithm is completely encapsulated in Strategy classes. Therefore, the implementation of an algorithm can be changed without affecting the Context class.&lt;br /&gt;
# Strategy Pattern can be used instead of sub-classing the Context class. Inheritance hardwires the behavior with the Context and the behavior cannot be changed dynamically.&lt;br /&gt;
# The same Strategy object can be strategically shared between different Context objects. However, the shared Strategy object should not maintain states across invocations.&lt;br /&gt;
&lt;br /&gt;
== Demerits ==&lt;br /&gt;
&lt;br /&gt;
# The application must be aware of all the strategies to select the right one for the right situation.&lt;br /&gt;
# Strategy and Context classes may be tightly coupled. The Context must supply the relevant data to the Strategy for implementing the algorithm and sometimes, all the data passed by the Context may not be relevant to all the Concrete Strategies.&lt;br /&gt;
# Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviors, which some concrete Strategy classes might not implement.&lt;br /&gt;
# In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.&lt;br /&gt;
# Since, the Strategy object is created by the application in most cases; the Context has no control on lifetime of the Strategy object. However, the Context can make a local copy of the Strategy object. But, this increases the memory requirement and has a sure performance impact.&lt;br /&gt;
&lt;br /&gt;
== Related Patterns ==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Command_pattern Command] and [http://en.wikipedia.org/wiki/Bridge_Pattern Bridge] are two closely related patterns to Strategy. &lt;br /&gt;
&lt;br /&gt;
* A Command pattern encapsulates a single action, therefore it tends to have a single method with a rather generic signature. It often is intended to be stored for a longer time and to be executed later. Strategy, in contrast, is used to customize an algorithm. A strategy might have a number of methods specific to the algorithm. Most often strategies will be instantiated immediately before executing the algorithm, and discarded afterwards. &lt;br /&gt;
&lt;br /&gt;
* Bridge pattern is meant for structure, whereas the Strategy pattern is meant for behavior. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.&lt;br /&gt;
&lt;br /&gt;
== Alternatives == &lt;br /&gt;
&lt;br /&gt;
In some programming languages, such as those without polymorphism, the issues addressed by strategy pattern are handled through forms of reflection, such as the native function pointer or function delegate syntax.&lt;br /&gt;
&lt;br /&gt;
= Example &amp;amp; Illustration =&lt;br /&gt;
&lt;br /&gt;
Normally there are 3 types of classes and or objects that participate in 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;
&lt;br /&gt;
* ConcreteStrategy  &lt;br /&gt;
: implements the algorithm using the Strategy interface&lt;br /&gt;
&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;
&lt;br /&gt;
&lt;br /&gt;
[[Image:Strategy-Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For example lets take the general case of sorting a list. One may wish to use different sort strategies based on the length of the list as performance of these sort algorithms vary depending on the size of the list. Here SortStrategy will be the common interface to all supported sort algorithms (like QuickSort, MergeSort, SyncSort etc.,) and SortedList will be the context that lets Strategy access the list and operate on it.&lt;br /&gt;
&lt;br /&gt;
= Real World Problem =&lt;br /&gt;
&lt;br /&gt;
To demonstrate further let us consider an application that manages songs and playlists, and helps you copy/synchronize songs to you compact MP3 player or Mobile phone.The application accepts a playlist which has a catalog of MP3 files in different directories. &lt;br /&gt;
&lt;br /&gt;
Let critical actions performed on the files in playlist be renaming, copying or reencoding. Let each file to be copied/synchronized be represented by &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; object containing the source and destination paths. &lt;br /&gt;
&lt;br /&gt;
The actual action (rename/copy/reencode) that needs to be performed is given to the &amp;lt;code&amp;gt;FileJob&amp;lt;/code&amp;gt; as an object implementing the &amp;lt;code&amp;gt;FileOperation&amp;lt;/code&amp;gt; interface (which is the strategy). The application then has three different file operations: &amp;lt;code&amp;gt;CopyOperation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RenameOperation&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ReencodeOperation&amp;lt;/code&amp;gt; (which are the implementations). Here the file under the operations takes the role of context.&lt;br /&gt;
&lt;br /&gt;
== Java's Solution ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Ruby's Solution ==&lt;br /&gt;
&lt;br /&gt;
= Java versus Ruby =&lt;br /&gt;
== Comparision ==&lt;br /&gt;
== Where Ruby wins? ==&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
===Books===&lt;br /&gt;
#[http://www.amazon.com/Programming-Ruby-Pragmatic-Programmers-Second/dp/0974514055 Programming Ruby: The programmatic programmer’s guide]&lt;br /&gt;
#[http://www.amazon.com/Head-First-Design-Patterns/dp/0596007124 Head First Design Patterns]&lt;br /&gt;
&lt;br /&gt;
===Links===&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternStrategy.aspx Strategy Pattern - Data and Object Factory]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_(computer_science) Design Patterns - Wikipedia]&lt;br /&gt;
#[http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html Strategy for Success - Java World]&lt;br /&gt;
&lt;br /&gt;
===See Also===&lt;br /&gt;
&lt;br /&gt;
#[http://davidhayden.com/blog/dave/archive/2005/07/01/1875.aspx Strategy Design Pattern and Open-Closed Principle]&lt;br /&gt;
#[http://www.devshed.com/c/a/PHP/Introducing-the-Strategy-Pattern/ Strategy Pattern for PHP Explained]&lt;/div&gt;</summary>
		<author><name>Sbselvad</name></author>
	</entry>
</feed>