<?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=Kctreima</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=Kctreima"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Kctreima"/>
	<updated>2026-08-13T17:52:38Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9720</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 9 mdkt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9720"/>
		<updated>2007-11-20T02:46:23Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise credited.  If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied.  Thank you.&lt;br /&gt;
&lt;br /&gt;
=Assignment=&lt;br /&gt;
Two of the important Agile methodologies that we have not said much about in this course are collective ownership and continuous integration. What are the best definitions or examples you can find? Are these widely practiced? Are they controversial? Are there adherents of other design methodologies who consider them harmful? Investigate, through the Web and through the ACM DL.&lt;br /&gt;
&lt;br /&gt;
=Collective Ownership=&lt;br /&gt;
==Definition==&lt;br /&gt;
“Collective code ownership means that &amp;lt;em&amp;gt;everybody is responsible for all the code&amp;lt;/em&amp;gt;; this, in turn, means that &amp;lt;em&amp;gt;everybody is allowed to change any part of the code&amp;lt;/em&amp;gt;.”&lt;br /&gt;
&lt;br /&gt;
==Implications==&lt;br /&gt;
*Everyone owns all of the code&lt;br /&gt;
**Anyone can change any code anywhere&lt;br /&gt;
**No personal ownership of modules&lt;br /&gt;
**No egoless programming either&lt;br /&gt;
*Everyone is permitted access to all the code so everyone has a stake in knowing all of the code&lt;br /&gt;
*Requires deserved trust&lt;br /&gt;
*Communication is very important. If code is property of the whole team, comments are also used to enforce communication.&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
*A major advantage claimed for collective ownership is that it speeds up the development process, because if an error occurs in the code any programmer may fix it.&lt;br /&gt;
&lt;br /&gt;
*All code gets the benefit of many people's attention, which increases code quality and reduces defects&lt;br /&gt;
&lt;br /&gt;
*Pair programming is usually used in conjunction with collective ownership: by working in different pairs, all the programmers get to see all the parts of the code. Pairing will be more difficult if the code any pair is working on is 'owned' by only one member. The non-owner of the pair may not feel comfortable suggesting or making changes to code that they don't have rights to. In collective ownership the code is ‘owned’ by everyone.&lt;br /&gt;
&lt;br /&gt;
*It requires a lot of interaction (especially with pairing) and verbal communication. It would be difficult for a non-contributing programmer to hide out for long in this environment&lt;br /&gt;
&lt;br /&gt;
===Problems with individual ownership that collective ownership addresses===&lt;br /&gt;
*When code is owned by individuals, required features are often put in the wrong place, as one programmer discovers that he needs a feature somewhere in code that he does not own. The owner is too busy to do it, so the programmer puts the feature in his own code, where it does not belong. This leads to ugly, hard-to-maintain code, full of duplication and with low (bad) cohesion. This is less likely to happen when every programmer is responsible for and has access to every piece of code.&lt;br /&gt;
*Individuals could become singularly responsible for specific areas of the project in individual ownership. This can pose a problem when any one individual leaves the project. The degree to which this may affect the project varies, depending on how well the individual has documented the program and on how much time there is to get somebody else trained before the individual leaves. When everyone has a stake in knowing every piece of code, as in a collective ownership scenario, this danger is less.&lt;br /&gt;
*Another problem is that modules that are too insular may become ‘miniprojects’ consisting of just a few developers each. Each miniproject may have its own set of coding styles, designs, politics, feature wars, and preferred technologies. Integrating these miniprojects into one collective, consistent product might prove to be impossible, depending how much they have been allowed to diverge. The problem worsens when the project scales up, particularly when not all the developers will fit into one room. In collective ownership the likelihood of very divergent coding practices amongst different groups is less likely.&lt;br /&gt;
*When only specific individuals are familiar with certain portions of the codebase, others on the team can be held up waiting for changes to be made. In addition, the amount of work needed for different sections of a project can change during the course of development. If individual ownership of each section is promoted too much, work loads can become lopsided&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
*By giving every programmer the right to change the code, there is risk of errors being introduced by programmers who think they know what they are doing, but do not foresee certain dependencies&lt;br /&gt;
&lt;br /&gt;
*The practice of quickly dipping into a related piece of code to make a quick change can be dangerous, because quick changes can often times create side-effect bugs.&lt;br /&gt;
&lt;br /&gt;
*Collective ownership could be a problem if people worked blindly on code they did not understand.&lt;br /&gt;
&lt;br /&gt;
*If only collective ownership, and not the other extreme programming practices of pair programming, testing, and coding standards, everyone’s efforts will end in chaos&lt;br /&gt;
&lt;br /&gt;
*Pair programming and collective ownership does not bring out individual contributions (good or bad). Bonus plans and other types of performance rewards may have to be changed.&lt;br /&gt;
&lt;br /&gt;
*Another concern with collective ownership is building expertise with the application. There are limits to the familiarity an individual can have with the codebase. Individual ownership helps solve this problem by limiting the amount of code a programmer creates to promote expertise in certain areas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Continuous Integration=&lt;br /&gt;
==Definition==&lt;br /&gt;
&amp;quot;Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible.&amp;quot;  [http://martinfowler.com/articles/continuousIntegration.html -Martin Fowler]&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The practice of Continuous Integration (CI) started as one of the twelve practices of Extreme Programming (XP).  It is made up of several sub-practices:&lt;br /&gt;
#Programmers must commit their changes frequently to the repository (at least daily).&lt;br /&gt;
#Changes will invoke an automated build.  &lt;br /&gt;
#The automated build must test the changes&lt;br /&gt;
&lt;br /&gt;
These practices in turn generate (possibly) new dependencies:&lt;br /&gt;
#Use a version-controlled source code repository, such as Subversion or CVS&lt;br /&gt;
#Use a test framework, such as any of the XUnit frameworks.&lt;br /&gt;
#Use an automated tool to build several times per day (see below)&lt;br /&gt;
&lt;br /&gt;
===Automated CI Tools===&lt;br /&gt;
These tools generally build the project at regular intervals.  They also integrate with the version repository, report build success or failure, and often integrate with tools to report on test metrics.&lt;br /&gt;
*[http://cruisecontrol.sourceforge.net/index.html Cruise Control]&lt;br /&gt;
:The first and most popular Java-based tool&lt;br /&gt;
*[http://cruisecontrolrb.thoughtworks.com/ Cruise Control.rb]&lt;br /&gt;
:Cruise Control, the Ruby way&lt;br /&gt;
*[http://maven.apache.org/continuum Continuum]&lt;br /&gt;
:A CI tool used primarily in projects that use the Apache Maven build tool&lt;br /&gt;
*[http://code.google.com/p/vulcan/ Vulcan]&lt;br /&gt;
:Google's answer to CI&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
As with most things, continuous integration has its benefits and drawbacks.  The benefits far outweigh the drawbacks, though, so CI is adopted often even in projects that do not use other XP practices.[[#References|&amp;lt;sup&amp;gt;2, 3&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Benefits===&lt;br /&gt;
*Frequent commits ensure that everyone is using the latest version of the code (or as close to it as possible).&lt;br /&gt;
*Using a test framework ensures that the changes are tested whenever the project is built.  More testing -&amp;gt; bugs are found (and fixed) sooner -&amp;gt; lower cost.&lt;br /&gt;
*Since much of the process is automated, team members are automatically kept informed of the state of the project.&lt;br /&gt;
*Studies show that undergraduate students who learn about CI have a deeper understanding of source code management (SCM)[[#References|&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
*Although CI is widely used in industry, it is rarely taught in academia.[[#References|&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*CI itself is nearly universally accepted as a good thing, but automated CI tools can be misused.  Instead of taking the opportunity to speed up a slow build process, an automated CI tool can be abused as a crutch.  Programmers rely on the tool to tell them that the build fails because of their changes, rather than ensuring (before commit) that their changes do not break the build.[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Collective code ownership and continuous integration are two useful Agile practices taken from Extreme Programming, with each having benefits and drawbacks when paired (or not paired) with other Agile techniques.  Collective ownership by itself is not very useful, but can be powerful when used in conjunction with other Agile practices.  Continuous integration, though, can be very useful on its own.  Collective ownership is good for small or less complex projects, but can be dangerous for large projects that require deep domain expertise.  Continuous integration provides a framework to enhance the quality of software, but the use of automated tools can tempt programmers to become lazy, and not follow the practice as they should.  When used properly, both practices can enhance the software development process.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://martinfowler.com/articles/continuousIntegration.html Continuous Integration]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1151436&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Motivations and Measurements in an Agile Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1159759&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=6644938 Agile Customer Engagement: a Longitudinal Qualitative Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1134393&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Assessing Undergraduate Experience of Continuous Integration and Test-Driven Development]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=775368&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 What We Can Learn from Extreme Programming]&lt;br /&gt;
#[http://www.jamesshore.com/Blog/Why%20I%20Dont%20Like%20CruiseControl.html Why I Don't Like CruiseControl]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuous_integration Continuous Integration (Wikipedia)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/CruiseControl CruiseControl (Wikipedia)]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9253</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 9 mdkt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9253"/>
		<updated>2007-11-19T07:54:20Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise credited.  If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied.  Thank you.&lt;br /&gt;
&lt;br /&gt;
=Assignment=&lt;br /&gt;
Two of the important Agile methodologies that we have not said much about in this course are collective ownership and continuous integration. What are the best definitions or examples you can find? Are these widely practiced? Are they controversial? Are there adherents of other design methodologies who consider them harmful? Investigate, through the Web and through the ACM DL.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Collective Ownership=&lt;br /&gt;
==Definition==&lt;br /&gt;
“Collective code ownership means that &amp;lt;em&amp;gt;everybody is responsible for all the code&amp;lt;/em&amp;gt;; this, in turn, means that &amp;lt;em&amp;gt;everybody is allowed to change any part of the code&amp;lt;/em&amp;gt;.”&lt;br /&gt;
&lt;br /&gt;
==Implications==&lt;br /&gt;
*Everyone owns all of the code&lt;br /&gt;
**Anyone can change any code anywhere&lt;br /&gt;
**No personal ownership of modules&lt;br /&gt;
**No egoless programming either&lt;br /&gt;
*Everyone is permitted access to all the code so everyone has a stake in knowing all of the code&lt;br /&gt;
*Requires deserved trust&lt;br /&gt;
*Communication is very important. If code is property of the whole team, comments are also used to enforce communication.&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
*A major advantage claimed for collective ownership is that it speeds up the development process, because if an error occurs in the code any programmer may fix it.&lt;br /&gt;
&lt;br /&gt;
*All code gets the benefit of many people's attention, which increases code quality and reduces defects&lt;br /&gt;
&lt;br /&gt;
*Pair programming is usually used in conjunction with collective ownership: by working in different pairs, all the programmers get to see all the parts of the code. Pairing will be more difficult if the code any pair is working on is 'owned' by only one member. The non-owner of the pair may not feel comfortable suggesting or making changes to code that they don't have rights to. In collective ownership the code is ‘owned’ by everyone.&lt;br /&gt;
&lt;br /&gt;
*It requires a lot of interaction (especially with pairing) and verbal communication. It would be difficult for a non-contributing programmer to hide out for long in this environment&lt;br /&gt;
&lt;br /&gt;
===Problems with individual ownership that collective ownership addresses===&lt;br /&gt;
*When code is owned by individuals, required features are often put in the wrong place, as one programmer discovers that he needs a feature somewhere in code that he does not own. The owner is too busy to do it, so the programmer puts the feature in his own code, where it does not belong. This leads to ugly, hard-to-maintain code, full of duplication and with low (bad) cohesion. This is less likely to happen when every programmer is responsible for and has access to every piece of code.&lt;br /&gt;
*Individuals could become singularly responsible for specific areas of the project in individual ownership. This can pose a problem when any one individual leaves the project. The degree to which this may affect the project varies, depending on how well the individual has documented the program and on how much time there is to get somebody else trained before the individual leaves. When everyone has a stake in knowing every piece of code, as in a collective ownership scenario, this danger is less.&lt;br /&gt;
*Another problem is that modules that are too insular may become ‘miniprojects’ consisting of just a few developers each. Each miniproject may have its own set of coding styles, designs, politics, feature wars, and preferred technologies. Integrating these miniprojects into one collective, consistent product might prove to be impossible, depending how much they have been allowed to diverge. The problem worsens when the project scales up, particularly when not all the developers will fit into one room. In collective ownership the likelihood of very divergent coding practices amongst different groups is less likely.&lt;br /&gt;
*When only specific individuals are familiar with certain portions of the codebase, others on the team can be held up waiting for changes to be made. In addition, the amount of work needed for different sections of a project can change during the course of development. If individual ownership of each section is promoted too much, work loads can become lopsided&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
*By giving every programmer the right to change the code, there is risk of errors being introduced by programmers who think they know what they are doing, but do not foresee certain dependencies&lt;br /&gt;
&lt;br /&gt;
*The practice of quickly dipping into a related piece of code to make a quick change can be dangerous, because quick changes can often times create side-effect bugs.&lt;br /&gt;
&lt;br /&gt;
*Collective ownership could be a problem if people worked blindly on code they did not understand.&lt;br /&gt;
&lt;br /&gt;
*If only collective ownership, and not the other extreme programming practices of pair programming, testing, and coding standards, everyone’s efforts will end in chaos&lt;br /&gt;
&lt;br /&gt;
*Pair programming and collective ownership does not bring out individual contributions (good or bad). Bonus plans and other types of performance rewards may have to be changed.&lt;br /&gt;
&lt;br /&gt;
*Another concern with collective ownership is building expertise with the application. There are limits to the familiarity an individual can have with the codebase. Individual ownership helps solve this problem by limiting the amount of code a programmer creates to promote expertise in certain areas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Continuous Integration=&lt;br /&gt;
==Definition==&lt;br /&gt;
&amp;quot;Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible.&amp;quot;  [http://martinfowler.com/articles/continuousIntegration.html -Martin Fowler]&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The practice of Continuous Integration (CI) started as one of the twelve practices of Extreme Programming (XP).  It is made up of several sub-practices:&lt;br /&gt;
#Programmers must commit their changes frequently to the repository (at least daily).&lt;br /&gt;
#Changes will invoke an automated build.  &lt;br /&gt;
#The automated build must test the changes&lt;br /&gt;
&lt;br /&gt;
These practices in turn generate (possibly) new dependencies:&lt;br /&gt;
#Use a version-controlled source code repository, such as Subversion or CVS&lt;br /&gt;
#Use a test framework, such as any of the XUnit frameworks.&lt;br /&gt;
#Use an automated tool to build several times per day (see below)&lt;br /&gt;
&lt;br /&gt;
===Automated CI Tools===&lt;br /&gt;
These tools generally build the project at regular intervals.  They also integrate with the version repository, report build success or failure, and often integrate with tools to report on test metrics.&lt;br /&gt;
*[http://cruisecontrol.sourceforge.net/index.html Cruise Control]&lt;br /&gt;
:The first and most popular Java-based tool&lt;br /&gt;
*[http://cruisecontrolrb.thoughtworks.com/ Cruise Control.rb]&lt;br /&gt;
:Cruise Control, the Ruby way&lt;br /&gt;
*[http://maven.apache.org/continuum Continuum]&lt;br /&gt;
:A CI tool used primarily in projects that use the Apache Maven build tool&lt;br /&gt;
*[http://code.google.com/p/vulcan/ Vulcan]&lt;br /&gt;
:Google's answer to CI&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
As with most things, continuous integration has its benefits and drawbacks.  The benefits far outweigh the drawbacks, though, so CI is adopted often even in projects that do not use other XP practices.[[#References|&amp;lt;sup&amp;gt;2, 3&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Benefits===&lt;br /&gt;
*Frequent commits ensure that everyone is using the latest version of the code (or as close to it as possible).&lt;br /&gt;
*Using a test framework ensures that the changes are tested whenever the project is built.  More testing -&amp;gt; bugs are found (and fixed) sooner -&amp;gt; lower cost.&lt;br /&gt;
*Since much of the process is automated, team members are automatically kept informed of the state of the project.&lt;br /&gt;
*Studies show that undergraduate students who learn about CI have a deeper understanding of source code management (SCM)[[#References|&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
*Although CI is widely used in industry, it is rarely taught in academia.[[#References|&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*CI itself is nearly universally accepted as a good thing, but automated CI tools can be misused.  Instead of taking the opportunity to speed up a slow build process, an automated CI tool can be abused as a crutch.  Programmers rely on the tool to tell them that the build fails because of their changes, rather than ensuring (before commit) that their changes do not break the build.[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://martinfowler.com/articles/continuousIntegration.html Continuous Integration]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1151436&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Motivations and Measurements in an Agile Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1159759&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=6644938 Agile Customer Engagement: a Longitudinal Qualitative Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1134393&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Assessing Undergraduate Experience of Continuous Integration and Test-Driven Development]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=775368&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 What We Can Learn from Extreme Programming]&lt;br /&gt;
#[http://www.jamesshore.com/Blog/Why%20I%20Dont%20Like%20CruiseControl.html Why I Don't Like CruiseControl]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
*[http://en.wikipedia.org/wiki/Continuous_integration Continuous Integration (Wikipedia)]&lt;br /&gt;
*[http://en.wikipedia.org/wiki/CruiseControl CruiseControl (Wikipedia)]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9252</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 9 mdkt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9252"/>
		<updated>2007-11-19T07:51:03Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Continuous Integration */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise credited.  If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied.  Thank you.&lt;br /&gt;
&lt;br /&gt;
=Assignment=&lt;br /&gt;
Two of the important Agile methodologies that we have not said much about in this course are collective ownership and continuous integration. What are the best definitions or examples you can find? Are these widely practiced? Are they controversial? Are there adherents of other design methodologies who consider them harmful? Investigate, through the Web and through the ACM DL.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Collective Ownership=&lt;br /&gt;
==Definition==&lt;br /&gt;
“Collective code ownership means that &amp;lt;em&amp;gt;everybody is responsible for all the code&amp;lt;/em&amp;gt;; this, in turn, means that &amp;lt;em&amp;gt;everybody is allowed to change any part of the code&amp;lt;/em&amp;gt;.”&lt;br /&gt;
&lt;br /&gt;
==Implications==&lt;br /&gt;
*Everyone owns all of the code&lt;br /&gt;
**Anyone can change any code anywhere&lt;br /&gt;
**No personal ownership of modules&lt;br /&gt;
**No egoless programming either&lt;br /&gt;
*Everyone is permitted access to all the code so everyone has a stake in knowing all of the code&lt;br /&gt;
*Requires deserved trust&lt;br /&gt;
*Communication is very important. If code is property of the whole team, comments are also used to enforce communication.&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
*A major advantage claimed for collective ownership is that it speeds up the development process, because if an error occurs in the code any programmer may fix it.&lt;br /&gt;
&lt;br /&gt;
*All code gets the benefit of many people's attention, which increases code quality and reduces defects&lt;br /&gt;
&lt;br /&gt;
*Pair programming is usually used in conjunction with collective ownership: by working in different pairs, all the programmers get to see all the parts of the code. Pairing will be more difficult if the code any pair is working on is 'owned' by only one member. The non-owner of the pair may not feel comfortable suggesting or making changes to code that they don't have rights to. In collective ownership the code is ‘owned’ by everyone.&lt;br /&gt;
&lt;br /&gt;
*It requires a lot of interaction (especially with pairing) and verbal communication. It would be difficult for a non-contributing programmer to hide out for long in this environment&lt;br /&gt;
&lt;br /&gt;
===Problems with individual ownership that collective ownership addresses===&lt;br /&gt;
*When code is owned by individuals, required features are often put in the wrong place, as one programmer discovers that he needs a feature somewhere in code that he does not own. The owner is too busy to do it, so the programmer puts the feature in his own code, where it does not belong. This leads to ugly, hard-to-maintain code, full of duplication and with low (bad) cohesion. This is less likely to happen when every programmer is responsible for and has access to every piece of code.&lt;br /&gt;
*Individuals could become singularly responsible for specific areas of the project in individual ownership. This can pose a problem when any one individual leaves the project. The degree to which this may affect the project varies, depending on how well the individual has documented the program and on how much time there is to get somebody else trained before the individual leaves. When everyone has a stake in knowing every piece of code, as in a collective ownership scenario, this danger is less.&lt;br /&gt;
*Another problem is that modules that are too insular may become ‘miniprojects’ consisting of just a few developers each. Each miniproject may have its own set of coding styles, designs, politics, feature wars, and preferred technologies. Integrating these miniprojects into one collective, consistent product might prove to be impossible, depending how much they have been allowed to diverge. The problem worsens when the project scales up, particularly when not all the developers will fit into one room. In collective ownership the likelihood of very divergent coding practices amongst different groups is less likely.&lt;br /&gt;
*When only specific individuals are familiar with certain portions of the codebase, others on the team can be held up waiting for changes to be made. In addition, the amount of work needed for different sections of a project can change during the course of development. If individual ownership of each section is promoted too much, work loads can become lopsided&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
*By giving every programmer the right to change the code, there is risk of errors being introduced by programmers who think they know what they are doing, but do not foresee certain dependencies&lt;br /&gt;
&lt;br /&gt;
*The practice of quickly dipping into a related piece of code to make a quick change can be dangerous, because quick changes can often times create side-effect bugs.&lt;br /&gt;
&lt;br /&gt;
*Collective ownership could be a problem if people worked blindly on code they did not understand.&lt;br /&gt;
&lt;br /&gt;
*If only collective ownership, and not the other extreme programming practices of pair programming, testing, and coding standards, everyone’s efforts will end in chaos&lt;br /&gt;
&lt;br /&gt;
*Pair programming and collective ownership does not bring out individual contributions (good or bad). Bonus plans and other types of performance rewards may have to be changed.&lt;br /&gt;
&lt;br /&gt;
*Another concern with collective ownership is building expertise with the application. There are limits to the familiarity an individual can have with the codebase. Individual ownership helps solve this problem by limiting the amount of code a programmer creates to promote expertise in certain areas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Continuous Integration=&lt;br /&gt;
==Definition==&lt;br /&gt;
&amp;quot;Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible.&amp;quot;  [http://martinfowler.com/articles/continuousIntegration.html -Martin Fowler]&lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
The practice of Continuous Integration (CI) started as one of the twelve practices of Extreme Programming (XP).  It is made up of several sub-practices:&lt;br /&gt;
#Programmers must commit their changes frequently to the repository (at least daily).&lt;br /&gt;
#Changes will invoke an automated build.  &lt;br /&gt;
#The automated build must test the changes&lt;br /&gt;
&lt;br /&gt;
These practices in turn generate (possibly) new dependencies:&lt;br /&gt;
#Use a version-controlled source code repository, such as Subversion or CVS&lt;br /&gt;
#Use a test framework, such as any of the XUnit frameworks.&lt;br /&gt;
#Use an automated tool to build several times per day (see below)&lt;br /&gt;
&lt;br /&gt;
===Automated CI Tools===&lt;br /&gt;
These tools generally build the project at regular intervals.  They also integrate with the version repository, report build success or failure, and often integrate with tools to report on test metrics.&lt;br /&gt;
*[http://cruisecontrol.sourceforge.net/index.html Cruise Control]&lt;br /&gt;
:The first and most popular Java-based tool&lt;br /&gt;
*[http://cruisecontrolrb.thoughtworks.com/ Cruise Control.rb]&lt;br /&gt;
:Cruise Control, the Ruby way&lt;br /&gt;
*[http://maven.apache.org/continuum Continuum]&lt;br /&gt;
:A CI tool used primarily in projects that use the Apache Maven build tool&lt;br /&gt;
*[http://code.google.com/p/vulcan/ Vulcan]&lt;br /&gt;
:Google's answer to CI&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
As with most things, continuous integration has its benefits and drawbacks.  The benefits far outweigh the drawbacks, though, so CI is adopted often even in projects that do not use other XP practices.[[#References|&amp;lt;sup&amp;gt;2, 3&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Benefits===&lt;br /&gt;
*Frequent commits ensure that everyone is using the latest version of the code (or as close to it as possible).&lt;br /&gt;
*Using a test framework ensures that the changes are tested whenever the project is built.  More testing -&amp;gt; bugs are found (and fixed) sooner -&amp;gt; lower cost.&lt;br /&gt;
*Since much of the process is automated, team members are automatically kept informed of the state of the project.&lt;br /&gt;
*Studies show that undergraduate students who learn about CI have a deeper understanding of source code management (SCM)[[#References|&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
===Drawbacks===&lt;br /&gt;
*Although CI is widely used in industry, it is rarely taught in academia.[[#References|&amp;lt;sup&amp;gt;5&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*CI itself is nearly universally accepted as a good thing, but automated CI tools can be misused.  Instead of taking the opportunity to speed up a slow build process, an automated CI tool can be abused as a crutch.  Programmers rely on the tool to tell them that the build fails because of their changes, rather than ensuring (before commit) that their changes do not break the build.[[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://martinfowler.com/articles/continuousIntegration.html Continuous Integration]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1151436&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Motivations and Measurements in an Agile Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1159759&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=6644938 Agile Customer Engagement: a Longitudinal Qualitative Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1134393&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Assessing Undergraduate Experience of Continuous Integration and Test-Driven Development]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=775368&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 What We Can Learn from Extreme Programming]&lt;br /&gt;
#[http://www.jamesshore.com/Blog/Why%20I%20Dont%20Like%20CruiseControl.html Why I Don't Like CruiseControl]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9251</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 9 mdkt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=9251"/>
		<updated>2007-11-19T07:45:13Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise credited.  If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied.  Thank you.&lt;br /&gt;
&lt;br /&gt;
=Assignment=&lt;br /&gt;
Two of the important Agile methodologies that we have not said much about in this course are collective ownership and continuous integration. What are the best definitions or examples you can find? Are these widely practiced? Are they controversial? Are there adherents of other design methodologies who consider them harmful? Investigate, through the Web and through the ACM DL.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Collective Ownership=&lt;br /&gt;
==Definition==&lt;br /&gt;
“Collective code ownership means that &amp;lt;em&amp;gt;everybody is responsible for all the code&amp;lt;/em&amp;gt;; this, in turn, means that &amp;lt;em&amp;gt;everybody is allowed to change any part of the code&amp;lt;/em&amp;gt;.”&lt;br /&gt;
&lt;br /&gt;
==Implications==&lt;br /&gt;
*Everyone owns all of the code&lt;br /&gt;
**Anyone can change any code anywhere&lt;br /&gt;
**No personal ownership of modules&lt;br /&gt;
**No egoless programming either&lt;br /&gt;
*Everyone is permitted access to all the code so everyone has a stake in knowing all of the code&lt;br /&gt;
*Requires deserved trust&lt;br /&gt;
*Communication is very important. If code is property of the whole team, comments are also used to enforce communication.&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
*A major advantage claimed for collective ownership is that it speeds up the development process, because if an error occurs in the code any programmer may fix it.&lt;br /&gt;
&lt;br /&gt;
*All code gets the benefit of many people's attention, which increases code quality and reduces defects&lt;br /&gt;
&lt;br /&gt;
*Pair programming is usually used in conjunction with collective ownership: by working in different pairs, all the programmers get to see all the parts of the code. Pairing will be more difficult if the code any pair is working on is 'owned' by only one member. The non-owner of the pair may not feel comfortable suggesting or making changes to code that they don't have rights to. In collective ownership the code is ‘owned’ by everyone.&lt;br /&gt;
&lt;br /&gt;
*It requires a lot of interaction (especially with pairing) and verbal communication. It would be difficult for a non-contributing programmer to hide out for long in this environment&lt;br /&gt;
&lt;br /&gt;
===Problems with individual ownership that collective ownership addresses===&lt;br /&gt;
*When code is owned by individuals, required features are often put in the wrong place, as one programmer discovers that he needs a feature somewhere in code that he does not own. The owner is too busy to do it, so the programmer puts the feature in his own code, where it does not belong. This leads to ugly, hard-to-maintain code, full of duplication and with low (bad) cohesion. This is less likely to happen when every programmer is responsible for and has access to every piece of code.&lt;br /&gt;
*Individuals could become singularly responsible for specific areas of the project in individual ownership. This can pose a problem when any one individual leaves the project. The degree to which this may affect the project varies, depending on how well the individual has documented the program and on how much time there is to get somebody else trained before the individual leaves. When everyone has a stake in knowing every piece of code, as in a collective ownership scenario, this danger is less.&lt;br /&gt;
*Another problem is that modules that are too insular may become ‘miniprojects’ consisting of just a few developers each. Each miniproject may have its own set of coding styles, designs, politics, feature wars, and preferred technologies. Integrating these miniprojects into one collective, consistent product might prove to be impossible, depending how much they have been allowed to diverge. The problem worsens when the project scales up, particularly when not all the developers will fit into one room. In collective ownership the likelihood of very divergent coding practices amongst different groups is less likely.&lt;br /&gt;
*When only specific individuals are familiar with certain portions of the codebase, others on the team can be held up waiting for changes to be made. In addition, the amount of work needed for different sections of a project can change during the course of development. If individual ownership of each section is promoted too much, work loads can become lopsided&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
*By giving every programmer the right to change the code, there is risk of errors being introduced by programmers who think they know what they are doing, but do not foresee certain dependencies&lt;br /&gt;
&lt;br /&gt;
*The practice of quickly dipping into a related piece of code to make a quick change can be dangerous, because quick changes can often times create side-effect bugs.&lt;br /&gt;
&lt;br /&gt;
*Collective ownership could be a problem if people worked blindly on code they did not understand.&lt;br /&gt;
&lt;br /&gt;
*If only collective ownership, and not the other extreme programming practices of pair programming, testing, and coding standards, everyone’s efforts will end in chaos&lt;br /&gt;
&lt;br /&gt;
*Pair programming and collective ownership does not bring out individual contributions (good or bad). Bonus plans and other types of performance rewards may have to be changed.&lt;br /&gt;
&lt;br /&gt;
*Another concern with collective ownership is building expertise with the application. There are limits to the familiarity an individual can have with the codebase. Individual ownership helps solve this problem by limiting the amount of code a programmer creates to promote expertise in certain areas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Continuous Integration=&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
==Examples in Industry==&lt;br /&gt;
===Continuum===&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://martinfowler.com/articles/continuousIntegration.html Continuous Integration]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1151436&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Motivations and Measurements in an Agile Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1159759&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=6644938 Agile Customer Engagement: a Longitudinal Qualitative Case Study]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=1134393&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 Assessing Undergraduate Experience of Continuous Integration and Test-Driven Development]&lt;br /&gt;
#[http://www.lib.ncsu.edu:2162/ft_gateway.cfm?id=775368&amp;amp;type=pdf&amp;amp;coll=portal&amp;amp;dl=ACM&amp;amp;CFID=43728495&amp;amp;CFTOKEN=66449388 What We Can Learn from Extreme Programming]&lt;br /&gt;
#[http://www.jamesshore.com/Blog/Why%20I%20Dont%20Like%20CruiseControl.html Why I Don't Like CruiseControl]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=8932</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 9 mdkt</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_9_mdkt&amp;diff=8932"/>
		<updated>2007-11-18T16:09:38Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise credited.  If you doubt this, please indicate the section that you feel is not original and the source from which you believe it was directly copied.  Thank you.&lt;br /&gt;
&lt;br /&gt;
=Assignment=&lt;br /&gt;
Two of the important Agile methodologies that we have not said much about in this course are collective ownership and continuous integration. What are the best definitions or examples you can find? Are these widely practiced? Are they controversial? Are there adherents of other design methodologies who consider them harmful? Investigate, through the Web and through the ACM DL.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Collective Ownership=&lt;br /&gt;
==Definition==&lt;br /&gt;
“Collective code ownership means that &amp;lt;em&amp;gt;everybody is responsible for all the code&amp;lt;/em&amp;gt;; this, in turn, means that &amp;lt;em&amp;gt;everybody is allowed to change any part of the code&amp;lt;/em&amp;gt;.”&lt;br /&gt;
&lt;br /&gt;
==Implications==&lt;br /&gt;
*Everyone owns all of the code&lt;br /&gt;
**Anyone can change any code anywhere&lt;br /&gt;
**No personal ownership of modules&lt;br /&gt;
**No egoless programming either&lt;br /&gt;
*Everyone is permitted access to all the code so everyone has a stake in knowing all of the code&lt;br /&gt;
*Requires deserved trust&lt;br /&gt;
*Communication is very important. If code is property of the whole team, comments are also used to enforce communication.&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
*A major advantage claimed for collective ownership is that it speeds up the development process, because if an error occurs in the code any programmer may fix it.&lt;br /&gt;
&lt;br /&gt;
*All code gets the benefit of many people's attention, which increases code quality and reduces defects&lt;br /&gt;
&lt;br /&gt;
*Pair programming is usually used in conjunction with collective ownership: by working in different pairs, all the programmers get to see all the parts of the code. Pairing will be more difficult if the code any pair is working on is 'owned' by only one member. The non-owner of the pair may not feel comfortable suggesting or making changes to code that they don't have rights to. In collective ownership the code is ‘owned’ by everyone.&lt;br /&gt;
&lt;br /&gt;
*It requires a lot of interaction (especially with pairing) and verbal communication. It would be difficult for a non-contributing programmer to hide out for long in this environment&lt;br /&gt;
&lt;br /&gt;
===Problems with individual ownership that collective ownership addresses===&lt;br /&gt;
*When code is owned by individuals, required features are often put in the wrong place, as one programmer discovers that he needs a feature somewhere in code that he does not own. The owner is too busy to do it, so the programmer puts the feature in his own code, where it does not belong. This leads to ugly, hard-to-maintain code, full of duplication and with low (bad) cohesion. This is less likely to happen when every programmer is responsible for and has access to every piece of code.&lt;br /&gt;
*Individuals could become singularly responsible for specific areas of the project in individual ownership. This can pose a problem when any one individual leaves the project. The degree to which this may affect the project varies, depending on how well the individual has documented the program and on how much time there is to get somebody else trained before the individual leaves. When everyone has a stake in knowing every piece of code, as in a collective ownership scenario, this danger is less.&lt;br /&gt;
*Another problem is that modules that are too insular may become ‘miniprojects’ consisting of just a few developers each. Each miniproject may have its own set of coding styles, designs, politics, feature wars, and preferred technologies. Integrating these miniprojects into one collective, consistent product might prove to be impossible, depending how much they have been allowed to diverge. The problem worsens when the project scales up, particularly when not all the developers will fit into one room. In collective ownership the likelihood of very divergent coding practices amongst different groups is less likely.&lt;br /&gt;
*When only specific individuals are familiar with certain portions of the codebase, others on the team can be held up waiting for changes to be made. In addition, the amount of work needed for different sections of a project can change during the course of development. If individual ownership of each section is promoted too much, work loads can become lopsided&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
*By giving every programmer the right to change the code, there is risk of errors being introduced by programmers who think they know what they are doing, but do not foresee certain dependencies&lt;br /&gt;
&lt;br /&gt;
*The practice of quickly dipping into a related piece of code to make a quick change can be dangerous, because quick changes can often times create side-effect bugs.&lt;br /&gt;
&lt;br /&gt;
*Collective ownership could be a problem if people worked blindly on code they did not understand.&lt;br /&gt;
&lt;br /&gt;
*If only collective ownership, and not the other extreme programming practices of pair programming, testing, and coding standards, everyone’s efforts will end in chaos&lt;br /&gt;
&lt;br /&gt;
*Pair programming and collective ownership does not bring out individual contributions (good or bad). Bonus plans and other types of performance rewards may have to be changed.&lt;br /&gt;
&lt;br /&gt;
*Another concern with collective ownership is building expertise with the application. There are limits to the familiarity an individual can have with the codebase. Individual ownership helps solve this problem by limiting the amount of code a programmer creates to promote expertise in certain areas.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=Continuous Integration=&lt;br /&gt;
==Definition==&lt;br /&gt;
&lt;br /&gt;
==Examples in Industry==&lt;br /&gt;
===Continuum===&lt;br /&gt;
&lt;br /&gt;
==Controversies==&lt;br /&gt;
&lt;br /&gt;
==Benefits==&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7895</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7895"/>
		<updated>2007-10-28T13:59:19Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise cited.  If you doubt this, please provide the source from which you think it was copied.  Thank you.&lt;br /&gt;
=Assignment=&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
==MVC Architecture==&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
==Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
=Comparison of Ruby on Rails and Apache Struts=&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model-View-Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
=Advantages=&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages for the Developer==&lt;br /&gt;
&lt;br /&gt;
===Rails advantages for the developer===&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Struts advantages for the developer===&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*When comparing popular job search sites, employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12, 13, 14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages for the Final Application==&lt;br /&gt;
&lt;br /&gt;
===Rails advantages for the final application===&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Struts advantages for the final application===&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;16&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;17&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Rails and Struts each have their own strengths and weaknesses.  Rails lends itself to simple designs, rapid development, and improved maintainability.  Struts excels in areas of performance, scalability, and internationalization.  If one is trying to make an architecture choice for a project, many other considerations must be also taken into account, such as staffing, support, and other economic issues beyond the scope of this technical review.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://jobsearch.monster.com/Search.aspx?q=struts&amp;amp;fn=&amp;amp;lid=&amp;amp;re=130&amp;amp;cy=us&amp;amp;brd=1&amp;amp;JSNONREG=1 Monster.com - Job postings for Struts]&lt;br /&gt;
#[http://jobsearch.monster.com/Search.aspx?q=rails&amp;amp;fn=&amp;amp;lid=&amp;amp;re=130&amp;amp;cy=us&amp;amp;brd=1&amp;amp;JSNONREG=1 Monster.com - Job postings for Rails]&lt;br /&gt;
#[http://seeker.dice.com/jobsearch/servlet/JobSearch?op=300&amp;amp;FREE_TEXT=struts Dice.com - Job postings for Struts]&lt;br /&gt;
#[http://seeker.dice.com/jobsearch/servlet/JobSearch?op=300&amp;amp;FREE_TEXT=rails Dice.com - Job postings for Rails]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7894</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7894"/>
		<updated>2007-10-28T13:37:52Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;===Note to reviewers:===&lt;br /&gt;
All prose on this page is original, unless otherwise cited.  If you doubt this, please provide the source from which you think it was copied.  Thank you.&lt;br /&gt;
=Assignment=&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
==MVC Architecture==&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
==Ruby on Rails==&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
==Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
=Comparison of Ruby on Rails and Apache Struts=&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model-View-Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
=Advantages=&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages for the Developer==&lt;br /&gt;
&lt;br /&gt;
===Rails advantages for the developer===&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Struts advantages for the developer===&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Advantages for the Final Application==&lt;br /&gt;
&lt;br /&gt;
===Rails advantages for the final application===&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Struts advantages for the final application===&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
Rails and Struts each have their own strengths and weaknesses.  Rails lends itself to simple designs, rapid development, and improved maintainability.  Struts excels in areas of performance, scalability, and internationalization.  If one is trying to make an architecture choice for a project, many other considerations must be also taken into account, such as staffing, support, and other economic issues beyond the scope of this technical review.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
=See Also=&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7726</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7726"/>
		<updated>2007-10-25T02:51:30Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages for the Final Application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model-View-Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Rails and Struts each have their own strengths and weaknesses.  Rails lends itself to simple designs, rapid development, and improved maintainability.  Struts excels in areas of performance, scalability, and internationalization.  If one is trying to make an architecture choice for a project, many other considerations must be also taken into account, such as staffing, support, and other economic issues beyond the scope of this technical review.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7725</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7725"/>
		<updated>2007-10-25T02:50:39Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model-View-Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Rails and Struts each have their own strengths and weaknesses.  Rails lends itself to simple designs, rapid development, and improved maintainability.  Struts excels in areas of performance, scalability, and internationalization.  If one is trying to make an architecture choice for a project, many other considerations must be also taken into account, such as staffing, support, and other economic issues beyond the scope of this technical review.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7696</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7696"/>
		<updated>2007-10-25T02:11:18Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7693</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7693"/>
		<updated>2007-10-25T02:05:43Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintenance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;11, 12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, zero configuration files, and Rails' DRY mantra make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
*Since Rails includes almost everything you need for a web application (and there are packages readily available that include the missing pieces), there are fewer moving parts, leading to simpler designs, and therefore, again, a more maintainable solution.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.[[#References|&amp;lt;sup&amp;gt;15&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7680</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7680"/>
		<updated>2007-10-25T01:53:12Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;13, 14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add internationalization'''&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, and zero configuration files make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://wiki.rubyonrails.org/rails/pages/Internationalization Internationalization in Ruby on Rails]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7677</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7677"/>
		<updated>2007-10-25T01:51:13Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;13, 14&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add internationalization'''&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, and zero configuration files make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Rails offers no support for internationalization.&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7650</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7650"/>
		<updated>2007-10-25T01:38:03Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages for the Final Application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add internationalization'''&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, and zero configuration files make maintenance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Java has been shown to be faster than Ruby in many performance benchmark tests.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;12&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Complementary technologies related to scalability (fast web servers, clustering, etc.) have evolved significantly in the last two years for RoR, but do not yet reach the maturity level of corresponding technologies for Java/Struts.&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7600</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7600"/>
		<updated>2007-10-25T00:36:07Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, internationalization'''&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, and zero configuration files make maintainance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://shootout.alioth.debian.org/ The Computer Language Benchmarks Game]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7598</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7598"/>
		<updated>2007-10-25T00:34:38Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages for the Final Application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, internationalization'''&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby on Rails, such as a smaller code footprint, more readable and elegant code, and zero configuration files make maintainance easier since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7190</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7190"/>
		<updated>2007-10-24T06:38:42Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, internationalization'''&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby, such as a smaller code footprint, more readable and elegant code, make maintainance easier (from the programmer's perspective) since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7189</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7189"/>
		<updated>2007-10-24T06:37:05Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
'''note: lots of good stuff in here to include:  http://rewrite.rickbradley.com/pages/moving_to_rails/'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, internationalization'''&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby, such as a smaller code footprint, more readable and elegant code, make maintainance easier (from the programmer's perspective) since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://rewrite.rickbradley.com/pages/moving_to_rails/ Evaluation: moving from Java to Ruby on Rails for the CenterNet rewrite]&lt;br /&gt;
#[http://www.theserverside.com/tt/articles/article.tss?l=RailsHibernate Hibernate vs. Rails: The Persistence Showdown]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7183</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7183"/>
		<updated>2007-10-24T06:14:59Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
'''note: lots of good stuff in here to include:  http://rewrite.rickbradley.com/pages/moving_to_rails/'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
*Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&lt;br /&gt;
*Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&lt;br /&gt;
*Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&lt;br /&gt;
*Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&lt;br /&gt;
*Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&lt;br /&gt;
*Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
*With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&lt;br /&gt;
*Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&lt;br /&gt;
*Commercial support is more widely available in the Java community.[[#References|&amp;lt;sup&amp;gt;10&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;12, 13&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, internationalization'''&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
*URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&lt;br /&gt;
*Some of the virtues of Ruby, such as a smaller code footprint, more readable and elegant code, make maintainance easier (from the programmer's perspective) since there is less code to maintain, and it is more easily understood.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
*Struts is an older and more established framework, and is a proven technology.&lt;br /&gt;
*Though Struts itself does not offer a persistence framework, it is often paired with Hibernate.  Hibernate offers performance advantages over ActiveRecord, particularly when dealing with complex data models using outer joins.[[#References|&amp;lt;sup&amp;gt;11&amp;lt;/sup&amp;gt;]]&lt;br /&gt;
*Since there are currently more Struts developers than Rails developers, staffing is easier, which lends itself to easier maintenance from a manager's perspective.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7179</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7179"/>
		<updated>2007-10-24T04:43:53Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
'''note: lots of good stuff in here to include:  http://rewrite.rickbradley.com/pages/moving_to_rails/'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;10, 11&amp;lt;/sup&amp;gt;]]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, performance, internationalization, what else?'''&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;reword: less config, less repetition in code, generated core code on fly makes for smaller app, etc&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&amp;lt;/li&amp;gt;  &lt;br /&gt;
&amp;lt;li&amp;gt;Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Struts is an older and more established framework, and is a proven technology.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
#[http://www.monster.com/ Monster.com]&lt;br /&gt;
#[http://www.dice.com/ Dice.com]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7178</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 1 rl</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_1_rl&amp;diff=7178"/>
		<updated>2007-10-24T04:41:49Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Advantages for the Developer */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Assignment==&lt;br /&gt;
Compare Ruby on Rails with Apache Struts. The Apache Struts framework in the Java world occupies a position similar to Ruby on Rails, except that it is not as universally used. Compare the two. Which are the advantages of each for the developer? For the finished application?&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
Ruby on Rails and Apache Struts are both web application frameworks based on the Model-View-Controller (MVC) architecture.   &lt;br /&gt;
&lt;br /&gt;
===MVC Architecture===&lt;br /&gt;
&lt;br /&gt;
MVC separates an application into three components: the ''model'', the ''view'', and the ''controller''.  The model maintains the application state; it consists of both the data used by the application as well as the rules surrounding the use of that data.  The view generates the user interface, which allows the user to view and interact with the data in the model.  The controller takes input from the outside world, interprets the input, and manipulates the model accordingly.  Separating these three components, or concerns, makes the application code easier to understand, reuse, and maintain.&lt;br /&gt;
&lt;br /&gt;
===Ruby on Rails===&lt;br /&gt;
&lt;br /&gt;
Ruby on Rails, also known as &amp;quot;Rails&amp;quot; or &amp;quot;RoR&amp;quot;, is the standard Web application framework for the Ruby programming language.  It is an open source project that was released in 2004, and was created to make web application development quick and easy. &lt;br /&gt;
&lt;br /&gt;
All Rails applications use the MVC architecture.  Rails projects are created with a placeholder for each concern, and these concerns always interact in the same standard way. Models and controllers are Ruby classes, and views are written in eRuby (HTML with embedded Ruby).  The Ruby router directs requests to a particular controller and action based on the URL.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Ruby on Rails, visit the [http://www.rubyonrails.org/ Ruby on Rails homepage].&lt;br /&gt;
&lt;br /&gt;
===Apache Struts===&lt;br /&gt;
&lt;br /&gt;
Apache Struts is a Web application framework for the Java programming language, used to create Java 2 Platform, Enterprise Edition (J2EE) applications.  It is an open source project that was donated to Apache in 2000. Struts was introduced to encourage the use of the MVC architecture.  Struts provides a controller in the way of request and response handlers, and provides a tag library that developers can use to develop views.  The model is left up to the developer.&lt;br /&gt;
&lt;br /&gt;
For more basic information on Apache Struts, visit the [http://struts.apache.org/ Apache Struts homepage].&lt;br /&gt;
&lt;br /&gt;
==Comparison of Ruby on Rails and Apache Struts==&lt;br /&gt;
&lt;br /&gt;
Rails and Struts are similar in many ways.  They are both web application frameworks for their respective languages, and they both implement the Model View Controller architecture.  The figure below, taken from [http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?], provides a comparison of a typical Rails stack and a typical J2EE stack containing Struts.&lt;br /&gt;
&lt;br /&gt;
[[Image:stack.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you can see, the two stacks are fundamentally quite similar.  Note, however, that the Persistence (Model) layer of the Rails stack, Active Record, is actually part of the Rails framework, whereas Hibernate (an object-relational mapper for Java) is not part of Struts.  This is one key difference between Rails and Struts: Rails provides a complete MVC implementation and Struts does not.  In addition to not providing a Model implementation, Struts also does not fully provide a View.  As you can see in the stack, JSPs are used for the View, and these JSPs take advantage of a tag library provided by Struts.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In Rails, the DispatchServlet dispatches web requests to the controller and action specified in the URL.  For example, a request for a URL ending in /questions/show/3 will be routed to the QuestionsController class and the show method. By convention, Rails knows that this will be found in questions_controller.rb.  Assuming scaffolding is in use and no customization has been done to change the behavior, this url will cause the question with ID of 3 to be displayed to the end user. The ActionServlet in Struts, on the other hand, uses an XML configuration file to map requests to specific controllers and actions.  This typifies another key difference between Rails and Struts:  Where Rails relies on convention, reflection and discovery to determine what to do, Struts relies on configuration files.  Note also that actions in Rails are simply methods inside their respective controllers, but they require full classes in Struts.&lt;br /&gt;
&lt;br /&gt;
==Advantages==&lt;br /&gt;
&lt;br /&gt;
Both Rails and Struts offer all the advantages that come with using the MVC architecture.  The separation of concerns makes the code more maintainable, reusable, and understandable.  It allows developers to specialize, and facilitates parallel development.  Finally, it makes enhancements to the application easier to design and implement.  The two frameworks each also offer distinct advantages over the other, both for the developer and for the finished application.&lt;br /&gt;
&lt;br /&gt;
'''note: lots of good stuff in here to include:  http://rewrite.rickbradley.com/pages/moving_to_rails/'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Developer===&lt;br /&gt;
&lt;br /&gt;
Rails offers the following advantages for the developer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails can be installed in minutes, and a fully-functional skeleton application can be created and running in &amp;lt;em&amp;gt;seconds&amp;lt;/em&amp;gt;.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails provides database scaffolding out of the box.  Essentially, Rails will generate all of the code necessary to do basic database interactions, such as create, read, update, and delete entries.  This allows developers to quickly and easily begin working with their data model, and replace the generated code with customized versions piece by piece.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails favors ''Convention over Configuration.'' Rails uses sensible defaults, so as long as you follow convention, your application can be written with significantly less code and without the great number of configuration files seen in other web applications today.  For example, if you create a model class called ''Product'', Rails will also generate for you a controller class named ''ProductsController'', and will assume that your model is using a database table called ''products''.  You only need to configure the table name if you are using a different value.  Writing less code and doing less configuration saves developers time during design, development, test, and maintentance.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails has ''Don't Repeat Yourself'' as its mantra.  Information about the system should be expressed in only one place.  For example, the names of the columns of a database table need only be contained in the table itself.  Rails gathers this information through reflection and discovery.  Avoiding code repetition is always good for the developer.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the developer:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Java/Struts/J2EE has significantly more and better tooling available than Ruby/Rails.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Employment opportunities for Struts are of an order of magnitude greater than those for Rails.[[#References|&amp;lt;sup&amp;gt;10, 11&amp;lt;/sup&amp;gt;]]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages for the Final Application===&lt;br /&gt;
'''to do: add scalability, performance, internationalization, what else?'''&lt;br /&gt;
&amp;lt;p&amp;gt;&lt;br /&gt;
Rails offers the following advantages for the final application:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;reword: less config, less repetition in code, generated core code on fly makes for smaller app, etc&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Rails natively supports filters, which allow you to wrap actions with before-code and after-code, for concerns such as authentication, logging, etc.  Struts, on the other hand, requires SAIF (Struts Action Invocation Framework) in order to support filters.&amp;lt;/li&amp;gt;  &lt;br /&gt;
&amp;lt;li&amp;gt;Rails natively supports layouts for a common look and feel.  Struts on the other hand requires the use of Tiles (a separate Apache project) to do the same, and this in turn necessitates even more XML configuration.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;URLs in a Rails application are short and clear.  They are of the form &amp;lt;nowiki&amp;gt;http://host:port/controller/action/id&amp;lt;/nowiki&amp;gt;.  Default Struts, on the other hand, exposes the use of Struts to the user by showing *.do at the end of the URL.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Struts offers the following advantages for the final application:&lt;br /&gt;
&amp;lt;ul&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;Struts is an older and more established framework, and is a proven technology.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;With Struts, you are not locked into a specific model implementation.  You can select a model implementation that best suits your application's needs.&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ul&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#[http://www.ibm.com/developerworks/linux/library/wa-rubyonrails/ Ruby on Rails and J2EE: Is there room for both?]&lt;br /&gt;
#[http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html Rolling with Ruby on Rails Revisited]&lt;br /&gt;
#[http://www.rubyonrails.org/ Ruby on Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Model-view-controller Model-view-controller]&lt;br /&gt;
#[http://www.csc.ncsu.edu/faculty/gehringer/517/f07/material/index.php CSC517 Lecture 12: Ruby on Rails]&lt;br /&gt;
#[http://www.pragprog.com/titles/rails2 Agile Web Development with Rails]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Ruby_on_Rails Ruby on Rails Wikipedia entry]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Struts Apache Struts Wikipedia entry]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/115595 Rails vs Java Struts]&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
#[http://www.hibernate.org/ Hibernate]&lt;br /&gt;
#[http://struts.sourceforge.net/saif/index.html SAIF]&lt;br /&gt;
#[http://tiles.apache.org/ Apache Tiles]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5452</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5452"/>
		<updated>2007-10-10T21:44:25Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class [[#References|&amp;lt;sup&amp;gt;3&amp;lt;/sup&amp;gt;]].  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  In Java the Attack is an interface that must be implemented as concrete classes, and in Ruby, the attack is a Proc object which encapsulates a behavior [[#References|&amp;lt;sup&amp;gt;4&amp;lt;/sup&amp;gt;]].  Specific attacks are implemented as modules with which to extend a fighter [[#References|&amp;lt;sup&amp;gt;6&amp;lt;/sup&amp;gt;]].  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Boxer b = new Boxer();&lt;br /&gt;
       Wrestler w = new Wrestler();&lt;br /&gt;
       Fighter[] fighters = { b, w };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       b.setAttack(new RoundhouseKick());&lt;br /&gt;
       w.setAttack(new LegSweep());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack. Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, using its block construct and Proc objects it is possible to eliminate the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby allows new attack behaviors to be created on the fly at run time in addition to being included as modules at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
#Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&lt;br /&gt;
#Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&lt;br /&gt;
#Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004, p.34&lt;br /&gt;
#CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007, p.4&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&lt;br /&gt;
#[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&lt;br /&gt;
#[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5445</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5445"/>
		<updated>2007-10-10T20:32:57Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Succinctness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Boxer b = new Boxer();&lt;br /&gt;
       Wrestler w = new Wrestler();&lt;br /&gt;
       Fighter[] fighters = { b, w };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       b.setAttack(new RoundhouseKick());&lt;br /&gt;
       w.setAttack(new LegSweep());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack. Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, using its block construct and Proc objectsit is possible to eliminate the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby allows new attack behaviors to be created on the fly at run time in addition to being included as modules at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5442</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5442"/>
		<updated>2007-10-10T20:28:08Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Clarity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Boxer b = new Boxer();&lt;br /&gt;
       Wrestler w = new Wrestler();&lt;br /&gt;
       Fighter[] fighters = { b, w };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       b.setAttack(new RoundhouseKick());&lt;br /&gt;
       w.setAttack(new LegSweep());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack. Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5434</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5434"/>
		<updated>2007-10-10T20:08:39Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Boxer b = new Boxer();&lt;br /&gt;
       Wrestler w = new Wrestler();&lt;br /&gt;
       Fighter[] fighters = { b, w };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       b.setAttack(new RoundhouseKick());&lt;br /&gt;
       w.setAttack(new LegSweep());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5426</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5426"/>
		<updated>2007-10-10T19:50:51Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Boxer b = new Boxer();&lt;br /&gt;
       Wrestler w = new Wrestler();&lt;br /&gt;
       Fighter[] fighters = { b, w };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       b.setAttack(new LegSweep());&lt;br /&gt;
       w.setAttack(new RoundhouseKick());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5425</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5425"/>
		<updated>2007-10-10T19:48:35Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem found in software development. Often during the development of a class, behaviors and algorithms (the strategies) can become mixed into the class while implementing without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through [http://www.gotw.ca/publications/mill06.htm inheritance], but over time this could result in an overly complex class hierarchy. The strategy pattern is a design pattern that can be used to [http://courses.csail.mit.edu/6.170/old-www/2002-Fall/lectures/lecture-09.pdf decouple] the behavior and algorithms (the strategy) from the object being acted upon (the context).&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
&amp;lt;p&amp;gt;[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&amp;lt;/p&amp;gt;&lt;br /&gt;
The general structure of the strategy design pattern has a context class which contains a reference to the strategy class’s behavior interface. The context class can instantiate whichever behavior that it needs statically or at runtime. This design provides much weaker coupling between the context and behavior than if different context classes were derived for each type of behavior.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (&amp;quot;[http://hillside.net/patterns/DPBook/DPBook.html Design Patterns] – Elements of Reusable Software” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://martinfowler.com/books.html#refactoring Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many similar behaviors or uses different variations of the same algorithm. Instead of using conditional statements within the class to select the behavior or algorithm, use the strategy pattern to take advantage of polymorphism.&lt;br /&gt;
#A class that uses data structures related to a behavior of the class but not the class itself. The strategy pattern can encapsulate the data specific to the behavior.&lt;br /&gt;
#Many similar classes that differ only in a type of behavior. These classes can be combined into a single class with the behavior refactored into separate classes so that the behavior can be selected by the instantiated class.&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern instead when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern instead when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern instead when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       fighters[0].setAttack(new LegSweep());&lt;br /&gt;
       fighters[1].setAttack(new RoundhouseKick());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Judo player will attack with a leg sweep by default&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The kickboxer will attack with a roundhouse kick by default&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The boxer will attack with a jab by default&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The wrestler will attack with a shoot in by default&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 b = Boxer.new&lt;br /&gt;
 w = Wrestler.new&lt;br /&gt;
 fighters = [ b, w ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # Now swap in new behaviors&lt;br /&gt;
 b.attack = Proc.new { puts &amp;quot;Front Kick!&amp;quot; }&lt;br /&gt;
 include LegSweep&lt;br /&gt;
 w.attack = Proc.new { leg_sweep }&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995, pp. 315-323&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5313</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5313"/>
		<updated>2007-10-09T22:26:26Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==The Question==&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;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes, behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from the object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://www.refactoring.com/ Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
#A class that exhibits many behaviors&amp;lt;/li&amp;gt;&lt;br /&gt;
#A class that uses many variations of an algorithm&amp;lt;/li&amp;gt;&lt;br /&gt;
#A class that uses data structures related to a behavior but not the class&amp;lt;/li&amp;gt;&lt;br /&gt;
#Many similar classes that differ only in a type of behavior&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
#Allows classes to change behavior at runtime or design time&amp;lt;/li&amp;gt;&lt;br /&gt;
#Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/li&amp;gt;&lt;br /&gt;
#Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/li&amp;gt;&lt;br /&gt;
#A well known design pattern communicates the intent of the code more readily&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    // This method sets the fighter's default attack,&lt;br /&gt;
    // which will be different for each type of fighter&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Boxer will execute a Jab by default&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       // The Wrestler will execute a ShootIn by default&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       // Create two fighters with default attack types&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       // Fight three rounds&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
       // Now swap in new behaviors&lt;br /&gt;
       fighters[0].setAttack(new LegSweep());&lt;br /&gt;
       fighters[1].setAttack(new RoundhouseKick());&lt;br /&gt;
       for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
          fighters[fighter].fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.execute&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The JudoPlayer will execute a LegSweep by default&lt;br /&gt;
     super(LegSweep.new)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The KickBoxer will execute a RoundhouseKick by default&lt;br /&gt;
     super(RoundhouseKick.new)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Boxer will execute a Jab by default&lt;br /&gt;
     super(Jab.new)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   def initialize&lt;br /&gt;
     # The Wrestler will execute a ShootIn by default&lt;br /&gt;
     super(ShootIn.new)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 class RoundhouseKick&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 class ShootIn&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 class LegSweep&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 class Jab&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 # Create two fighters with default attack types&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ JudoPlayer.new, KickBoxer.new ]&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # now swap in new behaviors&lt;br /&gt;
 fighters[0].attack = ShootIn.new&lt;br /&gt;
 fighters[1].attack = Jab.new&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5217</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5217"/>
		<updated>2007-10-02T01:41:51Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Succinctness */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes, behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from the object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://www.refactoring.com/ Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that exhibits many behaviors&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses many variations of an algorithm&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses data structures related to a behavior but not the class&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Many similar classes that differ only in a type of behavior&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Allows classes to change behavior at runtime or design time&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A well known design pattern communicates the intent of the code more readily&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.execute&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
MethodMissing.rb&lt;br /&gt;
 module MissingMethod&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class RoundhouseKick&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class ShootIn&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class LegSweep&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class Jab&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 # Create two fighters assigning initial attack types&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ JudoPlayer.new(LegSweep.new), KickBoxer.new(RoundhouseKick.new) ]&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # now swap in new behaviors&lt;br /&gt;
 fighters[0].attack = ShootIn.new&lt;br /&gt;
 fighters[1].attack = Jab.new&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5216</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5216"/>
		<updated>2007-10-02T01:39:11Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes, behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from the object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://www.refactoring.com/ Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that exhibits many behaviors&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses many variations of an algorithm&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses data structures related to a behavior but not the class&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Many similar classes that differ only in a type of behavior&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Allows classes to change behavior at runtime or design time&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A well known design pattern communicates the intent of the code more readily&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.execute&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
MethodMissing.rb&lt;br /&gt;
 module MissingMethod&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class RoundhouseKick&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class ShootIn&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class LegSweep&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class Jab&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 # Create two fighters assigning initial attack types&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ JudoPlayer.new(LegSweep.new), KickBoxer.new(RoundhouseKick.new) ]&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # now swap in new behaviors&lt;br /&gt;
 fighters[0].attack = ShootIn.new&lt;br /&gt;
 fighters[1].attack = Jab.new&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, once the framework of the design is in place, there are many features of the Ruby language that could be used to make the implementation more concise than than the Java version.  For example, Ruby offers a means to simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5207</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5207"/>
		<updated>2007-10-02T01:25:04Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes, behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from the object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://www.refactoring.com/ Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that exhibits many behaviors&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses many variations of an algorithm&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses data structures related to a behavior but not the class&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Many similar classes that differ only in a type of behavior&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Allows classes to change behavior at runtime or design time&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A well known design pattern communicates the intent of the code more readily&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.execute&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
MethodMissing.rb&lt;br /&gt;
 module MissingMethod&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class RoundhouseKick&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class ShootIn&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class LegSweep&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class Jab&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 # Create two fighters assigning initial attack types&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ JudoPlayer.new(LegSweep.new), KickBoxer.new(RoundhouseKick.new) ]&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # now swap in new behaviors&lt;br /&gt;
 fighters[0].attack = ShootIn.new&lt;br /&gt;
 fighters[1].attack = Jab.new&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  Once the framework of the design is in place, there are many features of the Ruby language that could be used to make the implementation more succinct than than the Java version. &lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, Ruby does offer a means to further simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5205</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=5205"/>
		<updated>2007-10-02T01:21:54Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes, behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from the object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===Generic Strategy Class Diagram===&lt;br /&gt;
[[Image:Strategy Class Diagram.jpg|Strategy Class Diagram]]&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [http://www.refactoring.com/ Refactoring]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that exhibits many behaviors&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses many variations of an algorithm&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A class that uses data structures related to a behavior but not the class&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Many similar classes that differ only in a type of behavior&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Allows classes to change behavior at runtime or design time&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt; A well known design pattern communicates the intent of the code more readily&amp;lt;/li&amp;gt;&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.execute&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
MethodMissing.rb&lt;br /&gt;
 module MissingMethod&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(technique)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(jab)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   def initialize(technique)&lt;br /&gt;
     super(shoot_in)&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class RoundhouseKick&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class ShootIn&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class LegSweep&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 require 'MethodMissing'&lt;br /&gt;
 class Jab&lt;br /&gt;
   include MethodMissing&lt;br /&gt;
   def execute&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 # Create two fighters assigning initial attack types&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ JudoPlayer.new(LegSweep.new), KickBoxer.new(RoundhouseKick.new) ]&lt;br /&gt;
 # Fight three rounds&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
 # now swap in new behaviors&lt;br /&gt;
 fighters[0].attack = ShootIn.new&lt;br /&gt;
 fighters[1].attack = Jab.new&lt;br /&gt;
 fighters.each do |fighter|&lt;br /&gt;
   fighter.fight&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
Both languages can do the basic implementation of the design pattern framework with near equal clarity.  The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  Once the framework of the design is in place, there are many features of the Ruby language that could be used to make the implementation more succinct than than the Java version. &lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
Overall, the two implementations are fairly similar in their succinctness, but in general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains fewer lines of code than the Java implementation.  If you ignore packaging (Java package statements) though, there are actually more lines in the Ruby implementation.  However, Ruby does offer a means to further simplify the code by using its block construct and Proc objects.  This will work for simple strategy patterns and could allow for the elimination of the behavioral classes altogether.  To make a comparison with Java, the Java strategy pattern would need to be implemented using anonymous inner classes, in which case Ruby's more versatile block construct would be easier to implement and maintain.&lt;br /&gt;
&lt;br /&gt;
The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  The block idiom in Ruby would allow new attack behaviors to be created on the fly at run time in addition to being assigned at compile time.  This meta-programming capability allows flexibility that Java does not support.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
===Concluding Remarks===&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct than the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.  Choosing a design implementation may come down to familiarity with the language or design decisions outside the scope of this discussion.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4950</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4950"/>
		<updated>2007-10-01T06:49:51Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct that the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack.  Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
In general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains 10 fewer lines of code than the Java implementation (13 fewer if you don't count the method_missing method added to Fighter that is called if you ask him to execute an attack he hasn't learned).  However, if you ignore packaging (Java package statements), the difference is much less.  Considering that, since Ruby does not need Interfaces there is one less file in the Ruby implementation, there is hardly any difference at all.  The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  In Ruby, you can create a new attack simply by passing a Proc object to the Fighter's attack= method at run time.  That object can be either a method from a new Module, or created on the fly using Proc.new followed by a block.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4949</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4949"/>
		<updated>2007-10-01T06:24:10Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Strategy Pattern Implementation in Java and Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.  To see our Fighters in action, there is also a Fight class in Java, and a Fight script which can be run in Ruby.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct that the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack.  Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
In general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains 10 fewer lines of code than the Java implementation (13 fewer if you don't count the method_missing method added to Fighter that is called if you ask him to execute an attack he hasn't learned).  However, if you ignore packaging (Java package statements), the difference is much less.  Considering that, since Ruby does not need Interfaces there is one less file in the Ruby implementation, there is hardly any difference at all.  The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  In Ruby, you can create a new attack simply by passing a Proc object to the Fighter's attack= method at run time.  That object can be either a method from a new Module, or created on the fly using Proc.new followed by a block.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4948</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4948"/>
		<updated>2007-10-01T06:20:54Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter[] fighters = { new Boxer(), new Wrestler() };&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          for (int fighter = 0; fighter &amp;lt; fighters.length; fighter++) {&lt;br /&gt;
             fighters[fighter].fight();&lt;br /&gt;
          }&lt;br /&gt;
       }&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct that the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack.  Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
In general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains 10 fewer lines of code than the Java implementation (13 fewer if you don't count the method_missing method added to Fighter that is called if you ask him to execute an attack he hasn't learned).  However, if you ignore packaging (Java package statements), the difference is much less.  Considering that, since Ruby does not need Interfaces there is one less file in the Ruby implementation, there is hardly any difference at all.  The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  In Ruby, you can create a new attack simply by passing a Proc object to the Fighter's attack= method at run time.  That object can be either a method from a new Module, or created on the fly using Proc.new followed by a block.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4947</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4947"/>
		<updated>2007-10-01T06:08:17Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct that the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack.  Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
In general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains 10 fewer lines of code than the Java implementation (13 fewer if you don't count the method_missing method added to Fighter that is called if you ask him to execute an attack he hasn't learned).  However, if you ignore packaging (Java package statements), the difference is much less.  Considering that, since Ruby does not need Interfaces there is one less file in the Ruby implementation, there is hardly any difference at all.  The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  In Ruby, you can create a new attack simply by passing a Proc object to the Fighter's attack= method at run time.  That object can be either a method from a new Module, or created on the fly using Proc.new followed by a block.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Head First Design Patterns, Eric Freeman and Elisabeth Freeman with Kathy Sierra and Bert Bates, O'Reilly Media, 2004&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Programming Ruby, Dave Thomas with Chad Fowler and Andy Hunt, The Pragmatic Programmers, 2005&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;CSC/ECE517 Lecture Notes (lecture 10), Edward F. Gehringer, 2007&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/Strategy_pattern Strategy Pattern - Wikipedia]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&amp;amp;f=9&amp;amp;t=003846 Command vs Strategy Pattern? (OO, Patterns, UML and Refactoring forum at JavaRanch)]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://www.rubynoob.com/articles/2006/5/16/strategy-design-pattern-in-ruby Rubynoob Strategy Design Pattern in Ruby]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151744 Re: Head First Design Patterns - Strategy Pattern [was: Java/C# &amp;quot;interface&amp;quot; in Ruby ?]]&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4943</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4943"/>
		<updated>2007-10-01T05:48:06Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
It would be nice to be able to say that one implementation was always clearer or more succinct that the other, but as with most things, it is a compromise.  To get a little more clarity in Java, you sacrifice some succinctness, and in Ruby you can get more succinctness for the price of some clarity.&lt;br /&gt;
&lt;br /&gt;
===Clarity===&lt;br /&gt;
The Java implementation relies less on language idioms, making it somewhat more clear (but not always).  For example, to anyone familiar with O-O languages (but not necessarily Java), it is clear what each of the concrete Fighter types does, as well as the Attacks.  However, the abstract Fighter class might not be as obvious (but probably still should be).  In Ruby, the strategy is implemented as a Proc object, so code to &amp;quot;call&amp;quot; an attack may not be as self-commenting as to execute an Attack.  Also, it is obvious that setAttack(new LegSweep()) sets the attack to be a leg sweep, but the call to super { leg_sweep } may not be.&lt;br /&gt;
&lt;br /&gt;
===Succinctness===&lt;br /&gt;
In general, the Ruby implementation is more concise.  Ruby's built-in iterators make it easier to work with collections, like arrays of Fighters, or rounds in a Fight.  Also, the Ruby version contains 10 fewer lines of code than the Java implementation (13 fewer if you don't count the method_missing method added to Fighter that is called if you ask him to execute an attack he hasn't learned).  However, if you ignore packaging (Java package statements), the difference is much less.  Considering that, since Ruby does not need Interfaces there is one less file in the Ruby implementation, there is hardly any difference at all.  The real advantage to using Ruby becomes more apparent when you extend the design to model more real-world situations.  Then you can take advantage of its dynamic nature.  For example, let's consider when a Fighter wants to learn new techniques, and be able to choose which one to use.  In Java, you need to create new Attack implementations before you can use them in a Fight.  Then you need to create a mechanism to choose which attack to use.  In Ruby, you can create a new attack simply by passing a Proc object to the Fighter's attack= method at run time.  That object can be either a method from a new Module, or created on the fly using Proc.new followed by a block.  Also, if you wanted to combine several attacks into an arsenal from which the fighter could choose any attack, this could be easily accomplished using Ruby's arrays and built-in iterators.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4931</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4931"/>
		<updated>2007-10-01T04:20:34Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Strategy Pattern Implementation in Java and Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
For our example, we chose to model a video game where there could be different types of Fighters pitted against each other, as you might see in the [http://en.wikipedia.org/wiki/UFC UFC].  In our case, the strategy is the Attack.  Each Fighter has a different type of Attack according to his training.  The Fighter is therefore an &amp;quot;abstract&amp;quot; class.  In statically-typed Java, the class itself is declared as abstract, and in dynamic Ruby it must be extended (or else the Fighter will not know any techniques).  To keep things simple, our fighters are not very smart or well-trained, so they each only get one Attack.&lt;br /&gt;
&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4918</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4918"/>
		<updated>2007-10-01T04:01:00Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Class Diagram */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1bClasses.jpg|Strategy pattern class diagram]]&lt;br /&gt;
&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4911</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4911"/>
		<updated>2007-10-01T03:57:09Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Strategy Pattern Implementation in Java and Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
===Class Diagram===&lt;br /&gt;
[[Image:Wiki1b.Classes.jpg]]&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Wiki1bClasses.jpg&amp;diff=4909</id>
		<title>File:Wiki1bClasses.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Wiki1bClasses.jpg&amp;diff=4909"/>
		<updated>2007-10-01T03:55:43Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: The class diagram shows the strategy (Attack), different implementations of the strategy (RoundhouseKick, Jab, etc.), an abstract user of the strategy (Fighter), and different concrete extensions (Boxer, Wrestler, etc.).&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;The class diagram shows the strategy (Attack), different implementations of the strategy (RoundhouseKick, Jab, etc.), an abstract user of the strategy (Fighter), and different concrete extensions (Boxer, Wrestler, etc.).&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4908</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 ktrk</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_ktrk&amp;diff=4908"/>
		<updated>2007-10-01T03:48:28Z</updated>

		<summary type="html">&lt;p&gt;Kctreima: /* Strategy Pattern Implementation in Java and Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction==&lt;br /&gt;
&lt;br /&gt;
===What is the Strategy Pattern?===&lt;br /&gt;
The strategy pattern is a proven object oriented design pattern that can be used to solve a common coupling problem often found in software development. Often during the development of compositional classes behaviors and algorithms can become mixed into the class during development without realizing that these behaviors or algorithms may change, become more complex, or additional behaviors may need to be added. These changes could be implemented by extending the class's behavior through inheritance, but over time this could result in an overly complex class hierarchy. The strategy pattern is a design template that can be used to decouple the behavior and algorithms from object being acted upon.&lt;br /&gt;
&lt;br /&gt;
===When to Use the Strategy Pattern===&lt;br /&gt;
In the seminal book on design patterns by the Gang-of-Four (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides), situations (also labeled &amp;quot;code smells&amp;quot; by Martin Fowler in [[==References==|Refactoring]]) are described where the strategy design pattern could be used to architect a better solution. These include:&lt;br /&gt;
&amp;lt;p&amp;gt;1. A class that exhibits many behaviors&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. A class that uses many variations of an algorithm&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. A class that uses data structures related to a behavior but not the class&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. Many similar classes that differ only in a type of behavior&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Advantages of the Strategy Pattern===&lt;br /&gt;
By decoupling through the use of composition rather than inheritance, several advantages emerge that improve the maintainability and readability of the code.&lt;br /&gt;
&amp;lt;p&amp;gt;1. Allows classes to change behavior at runtime or design time&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2. Decreases code duplication among classes using variations of the same behavior [http://en.wikipedia.org/wiki/Strategy_pattern]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3. Behavior is better encapsulated by not being buried in its context [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StrategyPattern.htm]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4. A well known design pattern communicates the intent of the code more readily&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Similar Patterns===&lt;br /&gt;
&amp;lt;p&amp;gt;[http://en.wikipedia.org/wiki/State_pattern State], [http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm Command], and [http://en.wikipedia.org/wiki/Bridge_pattern Bridge]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the state pattern when the state of the object changes with a change in behavior.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Use the bridge pattern when a structural design is needed.&amp;lt;/p&amp;gt;&lt;br /&gt;
For a discussion of the strategy pattern compared to the state and bridge patterns see [http://lists.cs.uiuc.edu/pipermail/gang-of-4-patterns/2003-November/000064.html Strategy Pattern vs. Bridge Pattern].&lt;br /&gt;
&amp;lt;p&amp;gt;Use the command pattern when the invoking object (the context object in the state pattern) does not have knowlege of the recipient object (the behavior in the state pattern). The intermediary command object takes care of dispatching the request to the appropriate object.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Strategy Pattern Implementation in Java and Ruby==&lt;br /&gt;
===Java===&lt;br /&gt;
Fighter.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public abstract class Fighter {&lt;br /&gt;
    private Attack attack;&lt;br /&gt;
    public Fighter(){&lt;br /&gt;
       setAttack();&lt;br /&gt;
    }&lt;br /&gt;
    public Attack getAttack() {&lt;br /&gt;
       return attack;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAttack(Attack attack) {&lt;br /&gt;
       this.attack = attack;&lt;br /&gt;
    }&lt;br /&gt;
    protected abstract void setAttack();&lt;br /&gt;
    public void fight() {&lt;br /&gt;
       getAttack().execute();&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class JudoPlayer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new LegSweep());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
KickBoxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class KickBoxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new RoundhouseKick());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Boxer.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Boxer extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new Jab());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Wrestler.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Wrestler extends Fighter {&lt;br /&gt;
    public void setAttack() {&lt;br /&gt;
       setAttack(new ShootIn());&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Attack.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public interface Attack {&lt;br /&gt;
    public void execute();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class RoundhouseKick implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Roundhouse Kick!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
ShootIn.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class ShootIn implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Shoot In!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
LegSweep.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class LegSweep implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Leg Sweep!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Jab.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Jab implements Attack {&lt;br /&gt;
    public void execute() {&lt;br /&gt;
       System.out.println(&amp;quot;Jab!&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Fight.java:&lt;br /&gt;
 package csc517.wiki1b;&lt;br /&gt;
 public class Fight {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
       Fighter f1 = new Boxer();&lt;br /&gt;
       Fighter f2 = new Wrestler();&lt;br /&gt;
       for (int round = 0; round &amp;lt; 3; round++) {&lt;br /&gt;
          f1.fight();&lt;br /&gt;
          f2.fight();&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
===Ruby===&lt;br /&gt;
Fighter.rb:&lt;br /&gt;
 class Fighter&lt;br /&gt;
   attr_accessor :attack&lt;br /&gt;
   def initialize(&amp;amp;technique)&lt;br /&gt;
     @attack = technique&lt;br /&gt;
   end&lt;br /&gt;
   def fight&lt;br /&gt;
     @attack.call&lt;br /&gt;
   end&lt;br /&gt;
   def method_missing(method_name)&lt;br /&gt;
     puts &amp;quot;I don't know how to #{method_name}&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
JudoPlayer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'LegSweep'&lt;br /&gt;
 class JudoPlayer &amp;lt; Fighter&lt;br /&gt;
   include LegSweep&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { leg_sweep }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
KickBoxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'RoundhouseKick'&lt;br /&gt;
 class KickBoxer &amp;lt; Fighter&lt;br /&gt;
   include RoundhouseKick&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { roundhouse_kick }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Boxer.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'Jab'&lt;br /&gt;
 class Boxer &amp;lt; Fighter&lt;br /&gt;
   include Jab&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { jab }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Wrestler.rb:&lt;br /&gt;
 require 'Fighter'&lt;br /&gt;
 require 'ShootIn'&lt;br /&gt;
 class Wrestler &amp;lt; Fighter&lt;br /&gt;
   include ShootIn&lt;br /&gt;
   def initialize&lt;br /&gt;
     super { shoot_in }&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
RoundhouseKick.rb:&lt;br /&gt;
 module RoundhouseKick&lt;br /&gt;
   def roundhouse_kick&lt;br /&gt;
     puts &amp;quot;Roundhouse Kick!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
ShootIn.rb:&lt;br /&gt;
 module ShootIn&lt;br /&gt;
   def shoot_in&lt;br /&gt;
     puts &amp;quot;Shoot In!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
LegSweep.rb:&lt;br /&gt;
 module LegSweep&lt;br /&gt;
   def leg_sweep&lt;br /&gt;
     puts &amp;quot;Leg Sweep!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Jab.rb:&lt;br /&gt;
 module Jab&lt;br /&gt;
   def jab&lt;br /&gt;
     puts &amp;quot;Jab!&amp;quot;&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
Fight.rb:&lt;br /&gt;
 require 'Boxer'&lt;br /&gt;
 require 'Wrestler'&lt;br /&gt;
 require 'KickBoxer'&lt;br /&gt;
 require 'JudoPlayer'&lt;br /&gt;
 rounds = 3&lt;br /&gt;
 fighters = [ Boxer.new, Wrestler.new ]&lt;br /&gt;
 rounds.times do&lt;br /&gt;
   fighters.each do |fighter|&lt;br /&gt;
     fighter.fight&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Design Patterns, Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Addison-Wesley Pulishing, 1995&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Refactoring: Improving the Design of Existing Code, Martin Fowler, Addison-Wesley Publishing, 1999&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;http://en.wikipedia.org/wiki/Strategy_pattern&amp;lt;/p&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kctreima</name></author>
	</entry>
</feed>