<?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=Jvegafr</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=Jvegafr"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Jvegafr"/>
	<updated>2026-10-13T15:25:07Z</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_4_sa&amp;diff=10204</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=10204"/>
		<updated>2007-11-28T22:44:26Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
While it can be argued that having a class that has many interfaces violates the Single Responsibility Principle [http://www.objectmentor.com/resources/articles/srp.pdf SRP], there are many instances where having a class implementing different interfaces makes sense, e.g., a graphics class might need to be rendered to screen or serialized to a file.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
The following two examples are taken from Martin's paper, but have been ported to Java.&lt;br /&gt;
&lt;br /&gt;
== Security system ==&lt;br /&gt;
&lt;br /&gt;
Suppose you have a security system that has Door objects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now suppose you have a TimedDoor which needs to sound an Alarm when the door has been left open for too long. To implement this functionality the TimerDoor object communicates with a Timer object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Timer&lt;br /&gt;
{&lt;br /&gt;
  ...&lt;br /&gt;
  public void Register(int timeout, TimerClient* client) {&lt;br /&gt;
  ...&lt;br /&gt;
  }&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
interface TimerClient&lt;br /&gt;
{&lt;br /&gt;
  public void TimeOut();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We might be tempted to do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door extends TimerClient {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Door interface has been polluted with the Timer interface in order to provide the Timer interface to TimedDoor and all Door clients now also depend on TimerClient. To void this we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Unchanged from first listing&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door, TimerClient {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This conforms to ISP by providing the required separate interfaces.&lt;br /&gt;
&lt;br /&gt;
== ATM System ==&lt;br /&gt;
&lt;br /&gt;
Consider an ATM that needs to be very flexible and support screen, braille tablet and speech synthesizer user interfaces. We can abstract the main operations and use an interface which would be implemented by specific UI classes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface ATM_UI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class BrailleUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ScreenUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SpeechUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now consider also that each different transaction the ATM can perform are encapsulated in a Transaction object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Transaction {&lt;br /&gt;
  void execute();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class DepositTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.deposit(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class WithdrawalTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class TransferTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, fromAcct, toAcct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that this is precisely the situation that the ISP tells us to avoid. Each of the transactions is using a portion of the UI that no other object uses. This creates the possibility that changes to one of the derivatives of Transaction will force corresponding change to the UI, thereby affecting all the other derivatives of Transaction, and every other class that depends upon the UI interface. To fix the problem we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface DepositUI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface WithdrawalUI {&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface TransferUI {&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface ATM_UI extends DepositUI, WithdrawalUI, TransferUI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And have the individual Transaction objects only reference the needed interfaces.&lt;br /&gt;
&lt;br /&gt;
= Web Resources =&lt;br /&gt;
&lt;br /&gt;
We were unable to find many examples of ISP in the web, the reason might be that ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are very good, the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them. We have tried to address these concerns by reimplementing Martin's examples in Java above. While doing so we could also see how Java encourages ISP with its support of interfaces.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html] has an example in modern UML. The example illustrates how adding new functionality to an existing system might force us to apply ISP to accommodate future changes. &lt;br /&gt;
&lt;br /&gt;
A Java Boutique page [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html] is available with a very concise and clear example in UML. The example shows how a violation of ISP might manifests itself as one having to provide null implementations for methods from the &amp;quot;fat&amp;quot; interface.&lt;br /&gt;
 &lt;br /&gt;
[http://ifacethoughts.net/2006/03/28/interface-segregation-principle/] contains an interesting example because it relates to real-world objects, in this case a cell phone. Modern phones support lots of other functions besides handling phone calls, and each of these functions is handled through different interfaces.&lt;br /&gt;
&lt;br /&gt;
The Object Mentor website also provides a paper with a number of design principles summarized, including ISP [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDF]. The advantage of said paper is that the other principles are easily accessible and a designer can compare for advantages and trade-off where multiple principles may be applied.&lt;br /&gt;
&lt;br /&gt;
The extensive use of interfaces in the Java API libraries is an excellent example of ISP because it provides flexibility and extensibility.&lt;br /&gt;
&lt;br /&gt;
Still the best specific examples in our opinion are the ones found on the original paper by Martin.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445/ref=pd_bbs_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-1 Agile Software Development, Principles, Patterns, and Practices]&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ref=pd_bbs_sr_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-2 Agile Principles, Patterns, and Practices in C#]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign Principles Of Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?MaskingInterfaces Masking Interfaces]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?RolePattern Role Pattern]&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=10193</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=10193"/>
		<updated>2007-11-28T22:37:43Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
While it can be argued that having a class that has many interfaces violates the Single Responsibility Principle [http://www.objectmentor.com/resources/articles/srp.pdf SRP], there are many instances where having a class implementing different interfaces makes sense, e.g., a graphics class might need to be rendered to screen or serialized to a file.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
The following two examples are taken from Martin's paper, but have been ported to Java.&lt;br /&gt;
&lt;br /&gt;
== Security system ==&lt;br /&gt;
&lt;br /&gt;
Suppose you have a security system that has Door objects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now suppose you have a TimedDoor which needs to sound an Alarm when the door has been left open for too long. To implement this functionality the TimerDoor object communicates with a Timer object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Timer&lt;br /&gt;
{&lt;br /&gt;
  ...&lt;br /&gt;
  public void Register(int timeout, TimerClient* client) {&lt;br /&gt;
  ...&lt;br /&gt;
  }&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
interface TimerClient&lt;br /&gt;
{&lt;br /&gt;
  public void TimeOut();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We might be tempted to do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door extends TimerClient {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Door interface has been polluted with the Timer interface in order to provide the Timer interface to TimedDoor and all Door clients now also depend on TimerClient. To void this we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Unchanged from first listing&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door, TimerClient {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This conforms to ISP by providing the required separate interfaces.&lt;br /&gt;
&lt;br /&gt;
== ATM System ==&lt;br /&gt;
&lt;br /&gt;
Consider an ATM that needs to be very flexible and support screen, braille tablet and speech synthesizer user interfaces. We can abstract the main operations and use an interface which would be implemented by specific UI classes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface ATM_UI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class BrailleUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ScreenUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SpeechUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now consider also that each different transaction the ATM can perform are encapsulated in a Transaction object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Transaction {&lt;br /&gt;
  void execute();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class DepositTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.deposit(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class WithdrawalTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class TransferTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, fromAcct, toAcct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that this is precisely the situation that the ISP tells us to avoid. Each of the transactions is using a portion of the UI that no other object uses. This creates the possibility that changes to one of the derivatives of Transaction will force corresponding change to the UI, thereby affecting all the other derivatives of Transaction, and every other class that depends upon the UI interface. To fix the problem we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface DepositUI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface WithdrawalUI {&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface TransferUI {&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface ATM_UI extends DepositUI, WithdrawalUI, TransferUI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And have the individual Transaction objects only reference the needed interfaces.&lt;br /&gt;
&lt;br /&gt;
= Web Resources =&lt;br /&gt;
&lt;br /&gt;
We were unable to find many examples of ISP in the web, the reason might be that ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are very good, the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them. We have tried to address these concerns by reimplementing Martin's examples in Java above. While doing so we could also see how Java encourages ISP with its support of interfaces.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html] has an example in modern UML. The example illustrates how adding new functionality to an existing system might force us to apply ISP to accommodate future changes. &lt;br /&gt;
&lt;br /&gt;
A Java Boutique page [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html] is available with a very concise and clear example in UML. The example shows how a violation of ISP might manifests itself as one having to provide null implementations for methods from the &amp;quot;fat&amp;quot; interface.&lt;br /&gt;
 &lt;br /&gt;
[http://ifacethoughts.net/2006/03/28/interface-segregation-principle/] contains an interesting example because it relates to real-world objects, in this case a cell phone. Modern phones support lots of other functions besides handling phone calls, and each of these functions is handled through different interfaces.&lt;br /&gt;
&lt;br /&gt;
The Object Mentor website also provides a paper with a number of design principles summarized, including ISP [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDF]. The advantage of said paper is that the other principles are easily accessible and a designer can compare for advantages and trade-off where multiple principles may be applied.&lt;br /&gt;
&lt;br /&gt;
Still the best examples in our opinion are the ones found on the original paper by Martin.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445/ref=pd_bbs_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-1 Agile Software Development, Principles, Patterns, and Practices]&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ref=pd_bbs_sr_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-2 Agile Principles, Patterns, and Practices in C#]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign Principles Of Object Oriented Design]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?MaskingInterfaces Masking Interfaces]&lt;br /&gt;
&lt;br /&gt;
[http://c2.com/cgi/wiki?RolePattern Role Pattern]&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=10158</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=10158"/>
		<updated>2007-11-28T21:59:13Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
While it can be argued that having a class that has many interfaces violates the Single Responsibility Principle [http://c2.com/cgi/wiki?SingleResponsibilityPrinciple SRP], there are many instances where having a class implementing different interfaces makes sense, e.g., a graphics class might need to be rendered to screen or serialized to a file.&lt;br /&gt;
&lt;br /&gt;
= Examples =&lt;br /&gt;
&lt;br /&gt;
The following two examples are taken from Martin's paper, but have been ported to Java.&lt;br /&gt;
&lt;br /&gt;
== Security system ==&lt;br /&gt;
&lt;br /&gt;
Suppose you have a security system that has Door objects:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now suppose you have a TimedDoor which needs to sound an Alarm when the door has been left open for too long. To implement this functionality the TimerDoor object communicates with a Timer object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
public class Timer&lt;br /&gt;
{&lt;br /&gt;
  ...&lt;br /&gt;
  public void Register(int timeout, TimerClient* client) {&lt;br /&gt;
  ...&lt;br /&gt;
  }&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
interface TimerClient&lt;br /&gt;
{&lt;br /&gt;
  public void TimeOut();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We might be tempted to do the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Door extends TimerClient {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Door interface has been polluted with the Timer interface in order to provide the Timer interface to TimedDoor and all Door clients now also depend on TimerClient. To void this we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Unchanged from first listing&lt;br /&gt;
interface Door {&lt;br /&gt;
  public void Lock();&lt;br /&gt;
  public void Unlock();&lt;br /&gt;
  public boolean IsDoorOpen();&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class TimedDoor implements Door, TimerClient {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This conforms to ISP by providing the required separate interfaces.&lt;br /&gt;
&lt;br /&gt;
== ATM System ==&lt;br /&gt;
&lt;br /&gt;
Consider an ATM that needs to be very flexible and support screen, braille tablet and speech synthesizer user interfaces. We can abstract the main operations and use an interface which would be implemented by specific UI classes:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface ATM_UI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class BrailleUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class ScreenUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class SpeechUI implements ATM_UI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now consider also that each different transaction the ATM can perform are encapsulated in a Transaction object:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface Transaction {&lt;br /&gt;
  void execute();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class DepositTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.deposit(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class WithdrawalTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, acct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class TransferTransaction implements Transaction {&lt;br /&gt;
  void execute() {&lt;br /&gt;
    atm.withdraw(amt, fromAcct, toAcct);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that this is precisely the situation that the ISP tells us to avoid. Each of the transactions is using a portion of the UI that no other object uses. This creates the possibility that changes to one of the derivatives of Transaction will force corresponding change to the UI, thereby affecting all the other derivatives of Transaction, and every other class that depends upon the UI interface. To fix the problem we can do the following instead:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
interface DepositUI {&lt;br /&gt;
  void deposit(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface WithdrawalUI {&lt;br /&gt;
  void withdraw(double amt, double account);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface TransferUI {&lt;br /&gt;
  void transfer(double amt, double fromAccount, double toAccount);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface ATM_UI extends DepositUI, WithdrawalUI, TransferUI {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And have the individual Transaction objects only reference the needed interfaces.&lt;br /&gt;
&lt;br /&gt;
= Web Resources =&lt;br /&gt;
&lt;br /&gt;
We were unable to find many examples of ISP in the web, the reason might be that ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are very good, the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project has an example in modern UML [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html].&lt;br /&gt;
&lt;br /&gt;
A Java Boutique page is available with a very concise and clear example in UML [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html].&lt;br /&gt;
 &lt;br /&gt;
The following page contains an interesting example because it relates to real-world objects, in this case a cell phone [http://ifacethoughts.net/2006/03/28/interface-segregation-principle/].&lt;br /&gt;
&lt;br /&gt;
The Object Mentor website also provides a paper with a number of design principles summarized, including ISP [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDF]. The advantage of said paper is that the other principles are easily accessible and a designer can compare for advantages and trade-off where multiple principles may be applied.&lt;br /&gt;
&lt;br /&gt;
Still the best examples in our opinion are the ones found on the original paper by Martin.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445/ref=pd_bbs_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-1 Agile Software Development, Principles, Patterns, and Practices]&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ref=pd_bbs_sr_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-2 Agile Principles, Patterns, and Practices in C#]&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8937</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8937"/>
		<updated>2007-11-18T16:56:14Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
= Web Resources =&lt;br /&gt;
&lt;br /&gt;
We were unable to find many examples of ISP in the web, the reason might be that ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are very good, the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project has an example in modern UML [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html].&lt;br /&gt;
&lt;br /&gt;
A Java Boutique page is available with a very concise and clear example in UML [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html].&lt;br /&gt;
 &lt;br /&gt;
The following page contains an interesting example because it relates to real-world objects, in this case a cell phone [http://ifacethoughts.net/2006/03/28/interface-segregation-principle/].&lt;br /&gt;
&lt;br /&gt;
The Object Mentor website also provides a paper with a number of design principles summarized, including ISP [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDF]. The advantage of said paper is that the other principles are easily accessible and a designer can compare for advantages and trade-off where multiple principles may be applied.&lt;br /&gt;
&lt;br /&gt;
Still the best examples in our opinion are the ones found on the original paper by Martin.&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Software-Development-Principles-Patterns-Practices/dp/0135974445/ref=pd_bbs_1?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-1 Agile Software Development, Principles, Patterns, and Practices]&lt;br /&gt;
&lt;br /&gt;
[http://www.amazon.com/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ref=pd_bbs_sr_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1195404857&amp;amp;sr=8-2 Agile Principles, Patterns, and Practices in C#]&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8933</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8933"/>
		<updated>2007-11-18T16:24:16Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
= Web Resources =&lt;br /&gt;
&lt;br /&gt;
ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are good, the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project has an example in modern UML [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html].&lt;br /&gt;
&lt;br /&gt;
A Java Boutique page is available with a very concise and clear example in UML [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html].&lt;br /&gt;
 &lt;br /&gt;
The following page contains an interesting example because it relates to real-world objects, in this case a cell phone [http://ifacethoughts.net/2006/03/28/interface-segregation-principle/].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8930</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8930"/>
		<updated>2007-11-18T16:01:34Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
:&amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
:&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
:&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Examples=&lt;br /&gt;
&lt;br /&gt;
Interfaces are useful in designing real world systems, where every object can have multiple aspects. This aspect can be a facet, a behaviour, a role - any entity that qualifies the object with that aspect. Lets take a real world example - a modern cell phone (mobile) acts more than just a phone, it is a phone, a MP3 player and a video player. When I am using it as a MP3 player, I will like to specify the song I want to play and listen to it, nothing more nothing else. When I am using it as a mobile phone, I will like to provide the number and speak, nothing more nothing else. I will get extremely irritated if I am asked for a phone number when I want to listen to a song. It is better if only the relevant questions are asked.&lt;br /&gt;
The cell phone had interfaces of a phone and a MP3 player. ISP advises to keep these two interfaces completely independent of each other. This way the phone can be treated purely as a phone or as a MP3 player. Programmatically, I can use only the interface of a MP3 player in my method called playSong. In future even if the interface for the phone is modified, playSong would remain the same.&lt;br /&gt;
= Resources =&lt;br /&gt;
&lt;br /&gt;
ISP is very well explained in the original paper [http://www.objectmentor.com/resources/articles/isp.pdf (ISP)], which has two examples in C++ and an early version of UML. These examples use C++'s multiple inheritance due to the lack of real interfaces in the language. Each interface is defined using an abstract class with pure virtual methods. Even when these examples are good the use of C++ and the old version of UML might make it harder for people used to languages with support for real interfaces like Java or C# to understand them.&lt;br /&gt;
&lt;br /&gt;
The ISP page at Doodle Project has an example in modern UML [http://doodleproject.sourceforge.net/articles/2001/interfaceSegregationPrinciple.html].&lt;br /&gt;
&lt;br /&gt;
A Java Boutique page is available with a very concise and clear example in UML [http://javaboutique.internet.com/tutorials/JavaOO/interface_segregation.html].&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
Interface Segregation Principle's idea is to see that if there are two non-cohesive functionalities, keep them separate, so that they can be used independent of other. This avoids design of fat interfaces, and provides a clear design to the user (client). Break the functionalities into atomic interfaces that can be then individually accessed by the user.&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8925</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8925"/>
		<updated>2007-11-18T15:06:52Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
All the basic design principles converge on the use of abstraction for incorporating flexibility using loose coupling behavior. The Interface Segregation Principle (ISP) provides guidelines for designing interfaces to classes that must provide different services to different clients. The Interface Segregation Principle focuses on the cohesiveness of interfaces with respect to the clients that use them.&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
ISP was originally presented in a paper by Robert C. Martin in his ''Engineering Notebook'' column for the ''C++ Report''. The paper is still available at the Object Mentor website [http://www.objectmentor.com/resources/articles/isp.pdf].&lt;br /&gt;
&lt;br /&gt;
The essence of Integration Segregation Principle is that &lt;br /&gt;
&amp;lt;b&amp;gt;&amp;quot;Clients should not be forced to depend upon interfaces that they don’t use.&amp;quot;&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some other definitions of ISP are:&lt;br /&gt;
: &amp;quot;Many client specific interfaces are better than one general purpose interface&amp;quot;&lt;br /&gt;
•	&amp;quot;The dependency of one class to another one should depend on the smallest possible interface&amp;quot;&lt;br /&gt;
•	&amp;quot;Make fine grained interfaces that are client specific.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Examples&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Resources =&lt;br /&gt;
&lt;br /&gt;
1) http://www.objectmentor.com/resources/articles/isp.pdf&lt;br /&gt;
2) http://ifacethoughts.net/2006/03/28/interface-segregation-principle/&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8831</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8831"/>
		<updated>2007-11-17T23:46:46Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Definition =&lt;br /&gt;
&lt;br /&gt;
= Resources =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8830</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8830"/>
		<updated>2007-11-17T23:46:23Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Take the Interface Segregation principle and catalog the information on it available on the Web. We didn't cover it in class, but you can look it up on the Web or in the ACM DL. Find good descriptions and good, concise, understandable examples. Tell which you consider the best to present to a class.''&lt;br /&gt;
&lt;br /&gt;
= Definition&lt;br /&gt;
&lt;br /&gt;
= Resources&lt;br /&gt;
&lt;br /&gt;
= Further reading&lt;br /&gt;
&lt;br /&gt;
''Note: Santosh Gurijala (skgurija) and Agustin Vega-Frias (jvegafr) are the authors of this page.''&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8653</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 4 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_4_sa&amp;diff=8653"/>
		<updated>2007-11-17T03:20:36Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This is a skeleton page for Santosh and Agustin to work on Topic 4 of Wiki 3.&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=8136</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=8136"/>
		<updated>2007-10-30T01:13:45Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
The animation in the following link illustrates how an ORM system works:&lt;br /&gt;
&lt;br /&gt;
[http://www.service-architecture.com/object-relational-mapping/articles/transparent_persistence.html Transparent persistence in object-relational mapping]&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is ''a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores.''[http://www.ksc.com/article1.htm] The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class methods automatically from the database schema.&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
Despite of the shortcomings of some implementations, an ORM systems goes a long way about making application programming simpler and database agnostic.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
None of the ORM implementations is perfect for all jobs, hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting an ORM system:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Maintainability ===&lt;br /&gt;
* What happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML.&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, support, forums, community&lt;br /&gt;
* Maturity, frequency of updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
An excellent introduction to ORM can be found at http://www.chimu.com/publications/objectRelational/, a PDF version can be downloaded from http://www.chimu.com/publications/objectRelational/objectRelational.pdf.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=8116</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=8116"/>
		<updated>2007-10-30T00:21:52Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* How ORM works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
The animation in the following link illustrates how an ORM system works:&lt;br /&gt;
&lt;br /&gt;
[http://www.service-architecture.com/object-relational-mapping/articles/transparent_persistence.html Transparent persistence in object-relational mapping]&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class methods automatically from the database schema.&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
None of the ORM implementations is perfect for all jobs, hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting an ORM system:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Maintainability ===&lt;br /&gt;
* What happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML.&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, support, forums, community&lt;br /&gt;
* Maturity, frequency of updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
An excellent introduction to ORM can be found at http://www.chimu.com/publications/objectRelational/, a PDF version can be downloaded from http://www.chimu.com/publications/objectRelational/objectRelational.pdf.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7599</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7599"/>
		<updated>2007-10-25T00:35:06Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Rails Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class methods automatically from the database schema.&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
None of the ORM implementations is perfect for all jobs, hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting an ORM system:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Maintainability ===&lt;br /&gt;
* What happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML.&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, support, forums, community&lt;br /&gt;
* Maturity, frequency of updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
An excellent introduction to ORM can be found at http://www.chimu.com/publications/objectRelational/, a PDF version can be downloaded from http://www.chimu.com/publications/objectRelational/objectRelational.pdf.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7597</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7597"/>
		<updated>2007-10-25T00:33:32Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements uses Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class structures automatically from the database schema. &lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
None of the ORM implementations is perfect for all jobs, hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting an ORM system:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Maintainability ===&lt;br /&gt;
* What happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML.&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, support, forums, community&lt;br /&gt;
* Maturity, frequency of updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
An excellent introduction to ORM can be found at http://www.chimu.com/publications/objectRelational/, a PDF version can be downloaded from http://www.chimu.com/publications/objectRelational/objectRelational.pdf.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7592</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7592"/>
		<updated>2007-10-25T00:28:07Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Criteria for Selection */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements uses Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class structures automatically from the database schema. &lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
None of the ORM implementations is perfect for all jobs, hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting an ORM system:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML.&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= Other links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7591</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7591"/>
		<updated>2007-10-25T00:25:28Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Comparing ORM systems */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements uses Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class structures automatically from the database schema. &lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is very important consideration in regards to maintainability and learning curve. As far as flexibility and ease of use are concerned the ORM implementations for dynamic languages clearly win.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= Other links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7122</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=7122"/>
		<updated>2007-10-24T02:14:52Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping metadata (e.g. XML mapping files in Java Hibernate). In languages that support metaprogramming at runtime or compile time the mapping can be done automatically from information gathered from the database schema.&lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database by manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= Design Patterns addressing ORM =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
Active Record is part of the pattern language presented by Fowler in the book [http://www.amazon.com/exec/obidos/ASIN/0321127420 Patterns of Enterprise Application Architecture]. A summary of the other patterns in the language can be found [http://www.martinfowler.com/eaaCatalog/ here].&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/].&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
The pattern language includes patterns that deal with ORM from the architectural and behavioral points of view.&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
= ORM implementations =&lt;br /&gt;
&lt;br /&gt;
An extensive list of ORM systems divided by language targeted can be found [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software here]. A shorter representative list follows.&lt;br /&gt;
&lt;br /&gt;
== Rails Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Rails Active Record library implements uses Active Record and related patterns as a foundation to provide an extremely elegant and succinct ORM system. By using the power of metaprogramming available in Ruby, the system is able to gather information from the database and generates class structures automatically from the database schema. &lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM interface for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports inheritance, polymorphism, composition and the Java collections framework. It uses a dual-layer cache architecture to optimize performance.&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html] for mapping, caching, logging, etc.&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
OJB [http://db.apache.org/ojb/] allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS. Supports for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a base class or interface will return matches from derived classes, even if they are mapped to different database tables. OJB uses an XML based Object/Relational mapping. &lt;br /&gt;
&lt;br /&gt;
== Database Template Library ==&lt;br /&gt;
&lt;br /&gt;
The DTL [http://dtemplatelib.sourceforge.net/] is a C++ ORM system that aims to provide a database abstraction similar to the Standard Template Library. The mapping is performed using macros and template metaprogramming.&lt;br /&gt;
&lt;br /&gt;
== SQLObject ==&lt;br /&gt;
&lt;br /&gt;
SQLObject [http://sqlobject.org/] is an ORM for Python. Like the Ruby On Rails' Active Record library it takes advantage of the reflection and dynamic characteristics of Python to generate class structures from the database schemas&lt;br /&gt;
&lt;br /&gt;
= Comparing ORM systems =&lt;br /&gt;
&lt;br /&gt;
The major difference between the pattern languages and the specific implementations is that the former attempt to address other issues besides how to map the data to objects and vice versa. For example Crossing Chasms includes a Table Design Pattern that addresses the question of when is the best time to design the database schema. This is an important consideration specially for systems that will evolve, since it is harder to make big changes to the database schema once the system is in use. The pattern solves the problem by requiring a first-pass object behavioral modeling. It makes sense because in a new system we want to store the state of our objects, not adapt our objects to a specific database schema.&lt;br /&gt;
&lt;br /&gt;
Configuration is another difference between systems. While some languages support trivial configuration through their metaprogramming capabilities others require extensive configuration. This is also very important in regards to the learning curve.&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= Other links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;br /&gt;
# http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6746</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6746"/>
		<updated>2007-10-23T02:02:54Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Oracle TopLink */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. &lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database my manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/], which also adds associations through the use of meta programming macros and inheritance with the Single Table Inheritance pattern [http://martinfowler.com/eaaCatalog/singleTableInheritance.html].&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation as described in [1] are:&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports the following key OO features&lt;br /&gt;
* Hibernate supports natural OO idioms; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in the build procedure&lt;br /&gt;
* Hibernate is extremely high performance, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink [http://www.oracle.com/technology/products/ias/toplink/index.html] is a proprietary ORM system developed by Oracle. It supports the Java Persistence API and provides extensions [http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-extensions.html].&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
[http://java.sun.com/jdo]&lt;br /&gt;
&lt;br /&gt;
== Java Persistence API ==&lt;br /&gt;
&lt;br /&gt;
The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications.&lt;br /&gt;
[http://java.sun.com/javaee/technologies/persistence.jsp]&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
[http://db.apache.org/ojb/]&lt;br /&gt;
&lt;br /&gt;
== Enterprise Objects Framework (Apple) ==&lt;br /&gt;
&lt;br /&gt;
Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database.&lt;br /&gt;
The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance.&lt;br /&gt;
The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications.&lt;br /&gt;
[http://developer.apple.com/]&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6734</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6734"/>
		<updated>2007-10-23T01:51:07Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Hibernate */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. &lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database my manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/], which also adds associations through the use of meta programming macros and inheritance with the Single Table Inheritance pattern [http://martinfowler.com/eaaCatalog/singleTableInheritance.html].&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation as described in [1] are:&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate is a Java ORM and query service [http://www.hibernate.org]. The current version of the system can be used using different APIs:&lt;br /&gt;
&lt;br /&gt;
* '''Native'''. The user uses XML metadata to help the Hibernate subsystem map table data and relationship to application classes and objects.&lt;br /&gt;
* '''Java Persistence API'''. Hibernate implements the Java Persistence API [http://java.sun.com/javaee/technologies/persistence.jsp] which defines a standard ORM for Java.&lt;br /&gt;
&lt;br /&gt;
Hibernate supports the following key OO features&lt;br /&gt;
* Hibernate supports natural OO idioms; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in the build procedure&lt;br /&gt;
* Hibernate is extremely high performance, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
[http://www.oracle.com/technology/products/ias/toplink/index.html]&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
[http://java.sun.com/jdo]&lt;br /&gt;
&lt;br /&gt;
== Java Persistence API ==&lt;br /&gt;
&lt;br /&gt;
The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications.&lt;br /&gt;
[http://java.sun.com/javaee/technologies/persistence.jsp]&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
[http://db.apache.org/ojb/]&lt;br /&gt;
&lt;br /&gt;
== Enterprise Objects Framework (Apple) ==&lt;br /&gt;
&lt;br /&gt;
Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database.&lt;br /&gt;
The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance.&lt;br /&gt;
The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications.&lt;br /&gt;
[http://developer.apple.com/]&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6726</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6726"/>
		<updated>2007-10-23T01:31:32Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Crossing Chasms pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. &lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database my manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/], which also adds associations through the use of meta programming macros and inheritance with the Single Table Inheritance pattern [http://martinfowler.com/eaaCatalog/singleTableInheritance.html].&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation as described in [1] are:&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design conference [PLoPD], the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
[http://www.hibernate.org]&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
[http://www.oracle.com/technology/products/ias/toplink/index.html]&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
[http://java.sun.com/jdo]&lt;br /&gt;
&lt;br /&gt;
== Java Persistence API ==&lt;br /&gt;
&lt;br /&gt;
The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications.&lt;br /&gt;
[http://java.sun.com/javaee/technologies/persistence.jsp]&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
[http://db.apache.org/ojb/]&lt;br /&gt;
&lt;br /&gt;
== Enterprise Objects Framework (Apple) ==&lt;br /&gt;
&lt;br /&gt;
Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database.&lt;br /&gt;
The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance.&lt;br /&gt;
The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications.&lt;br /&gt;
[http://developer.apple.com/]&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6719</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6719"/>
		<updated>2007-10-23T01:07:34Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. &lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database my manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
The best example of the implementation of the pattern is found in the Rails framework [http://ar.rubyonrails.com/], which also adds associations through the use of meta programming macros and inheritance with the Single Table Inheritance pattern [http://martinfowler.com/eaaCatalog/singleTableInheritance.html].&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation as described in [1] are:&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
[http://www.hibernate.org]&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
[http://www.oracle.com/technology/products/ias/toplink/index.html]&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
[http://java.sun.com/jdo]&lt;br /&gt;
&lt;br /&gt;
== Java Persistence API ==&lt;br /&gt;
&lt;br /&gt;
The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications.&lt;br /&gt;
[http://java.sun.com/javaee/technologies/persistence.jsp]&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
[http://db.apache.org/ojb/]&lt;br /&gt;
&lt;br /&gt;
== Enterprise Objects Framework (Apple) ==&lt;br /&gt;
&lt;br /&gt;
Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database.&lt;br /&gt;
The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance.&lt;br /&gt;
The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications.&lt;br /&gt;
[http://developer.apple.com/]&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6715</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6715"/>
		<updated>2007-10-23T00:56:53Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* How ORM works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. &lt;br /&gt;
&lt;br /&gt;
ORM systems typically provide the programmer with object factories that map class method calls to SQL queries that retrieve objects from the database. They also provide methods to store the object data back in the database after updates in the application. This ability of an ORM to allow the programmer to interact with the database my manipulating objects in the programming language is referred to as ''transparent persistence'', because the database access is seamless to the programmer.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler: ''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
[http://www.hibernate.org]&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
[http://www.oracle.com/technology/products/ias/toplink/index.html]&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
[http://java.sun.com/jdo]&lt;br /&gt;
&lt;br /&gt;
== Java Persistence API ==&lt;br /&gt;
&lt;br /&gt;
The Java Persistence API draws upon the best ideas from persistence technologies such as Hibernate, TopLink, and JDO. Customers now no longer face the choice between incompatible non-standard persistence models for object/relational mapping. The Java Persistence API contains a full object/relational mapping specification supporting the use of Java language metadata annotations and/or XML descriptors to define the mapping between Java objects and a relational database. It supports a rich, SQL-like query language (which is a significant extension upon EJB QL) for both static and dynamic queries. It also supports the use of pluggable persistence providers. The Java Persistence API is now the preferred persistence API for use with EJB 3.0 applications.&lt;br /&gt;
[http://java.sun.com/javaee/technologies/persistence.jsp]&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
[http://db.apache.org/ojb/]&lt;br /&gt;
&lt;br /&gt;
== Enterprise Objects Framework (Apple) ==&lt;br /&gt;
&lt;br /&gt;
Enterprise Objects Framework provides tools for defining an object model and mapping it to a data model. This allows to create objects that encapsulate both data and the methods for operating on that data, while taking advantage of the data access services provided by the Framework that make it possible for these objects to persist in a relational database.&lt;br /&gt;
The flexible, three-tier architecture provided by the Framework allows to build robust, scalable, client/server applications. Objects at each of the three tiers (user interface, enterprise objects, and data store) can be deployed to take advantage of network resources. For example, data might be stored in a relational database running on a fault-tolerant database server with gigabytes of disk storage, while enterprise objects run on high-end compute servers. Partitioning the application to make best use of available resources allows complex applications to achieve maximum performance.&lt;br /&gt;
The portions of the Framework can be used selectively to meet specific application requirements. For example, the components that provide users with the ability to interactively manipulate enterprise objects can be used by a non-database application to handle user interface refresh and undo. We can use a custom data store (such as a flat-file system) in place of a relational database to store data for enterprise objects. Or we can make use of the database adapters separate from the rest of the Framework components to provide direct access to relational databases for your applications.&lt;br /&gt;
[http://developer.apple.com/]&lt;br /&gt;
&lt;br /&gt;
= Criteria for Selection =&lt;br /&gt;
&lt;br /&gt;
There is no perfect single tool exists. Hence the most important thing when searching for the right tool is to define precisely which criteria are essential. Following is the summary of criteria which can be considered while selecting the ORM product:&lt;br /&gt;
&lt;br /&gt;
== ORM specific criteria ==&lt;br /&gt;
=== Basic features: ===&lt;br /&gt;
* Be able to use inheritance, create hierarchies between entities, and use polymorphism (we are using objects!). The tools can support a variety of combinations for tables and classes to allow these mechanisms.&lt;br /&gt;
* Handle any type of relations (1-1, 1-n, n-n)&lt;br /&gt;
* Support for transactions&lt;br /&gt;
* Aggregates (equivalent to SQL's SUM, AVG, MIN, MAX, COUNT)&lt;br /&gt;
* Support for grouping (SQL's GROUP BY)&lt;br /&gt;
* Supported databases. A big advantage of mapping tools is that they provide an abstraction of the underlying database engine. Most of them allow switching easily between RDBMSs &lt;br /&gt;
=== Flexibility ===&lt;br /&gt;
* Customization of queries. HQL is a strong point of Hibernate/NHibernate. We could also wish a dynamic mapping to be possible from developer provided SQL queries.&lt;br /&gt;
* Support any type of SQL joins (inner join, outer join)&lt;br /&gt;
* Be able to map a single object to data coming from multiple tables (joins, views). And Be able to dispatch the data from a single table to multiple objects.&lt;br /&gt;
=== Performance ===&lt;br /&gt;
* Lazy loading - he loading of some data is deferred until it's needed. For the data through relations and for some columns.&lt;br /&gt;
* Cache dynamically generated queries and Cache some data to avoid too many calls to the data source.&lt;br /&gt;
* Handle cascade updates. Deleting a master record should delete the linked details if wished so.&lt;br /&gt;
=== Evolution: ===&lt;br /&gt;
* Maintainability - what happens if the database schema changes? If I need to add a new collection?&lt;br /&gt;
* Possibility to move to a new mapping tool&lt;br /&gt;
* Serialization. Serialization can be used to persist data outside of the database. Serialization can be done into a binary format, or more important, in XML (&lt;br /&gt;
&lt;br /&gt;
== Non ORM specific criteria ==&lt;br /&gt;
* Price&lt;br /&gt;
* Performance&lt;br /&gt;
* Resource consumption (memory)&lt;br /&gt;
* Scalability&lt;br /&gt;
* Complexity (or simplicity...)&lt;br /&gt;
* Ease of use, time to be up and running&lt;br /&gt;
* Documentation quality, Support, forums, community&lt;br /&gt;
* Maturity, Frequency of the updates, bug fixes, evolutions&lt;br /&gt;
* Vendor's reputation and stability&lt;br /&gt;
* Support for multiple platforms&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;br /&gt;
&lt;br /&gt;
# http://ar.rubyonrails.com/&lt;br /&gt;
# http://www.ksc.com/article5.htm&lt;br /&gt;
# http://www.hibernate.org&lt;br /&gt;
# http://www.oracle.com/technology/products/ias/toplink/index.html&lt;br /&gt;
# http://java.sun.com/jdo&lt;br /&gt;
# http://java.sun.com/javaee/technologies/persistence.jsp&lt;br /&gt;
# http://db.apache.org/ojb/&lt;br /&gt;
# http://developer.apple.com/&lt;br /&gt;
# http://en.wikipedia.org/wiki/Object-relational_mapping&lt;br /&gt;
# http://c2.com/cgi-bin/wiki?ObjectRelationalToolComparison&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6464</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6464"/>
		<updated>2007-10-22T01:48:46Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* What is ORM? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
A comprehensive list of ORM systems by language is available at [http://en.wikipedia.org/wiki/List_of_object-relational_mapping_software List of object-relational mapping software]&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems typically provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler:&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6459</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6459"/>
		<updated>2007-10-22T01:40:20Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* How ORM works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. XML mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems typically provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler:&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6454</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6454"/>
		<updated>2007-10-22T01:24:37Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems like the Active Record implementation in Ruby provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row, or as described by its author Martin Fowler:&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' [http://www.martinfowler.com/eaaCatalog/activeRecord.html]&lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6453</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6453"/>
		<updated>2007-10-22T01:22:07Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Crossing Chasms pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems like the Active Record implementation in Ruby provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row (http://www.martinfowler.com/eaaCatalog/activeRecord.html):&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' &lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
Crossing Chasms is a large pattern language that as a whole addresses the issues faced by teams trying to build large client-server systems using Object Technology and Relational data stores. The authors, Kyle Brown and Bruce Whitenack, originally presented the language at the Second Pattern Languages of Program Design [PLoPD] conference, the proceedings of the conference were published as the book [http://www.amazon.com/Pattern-Languages-Program-Software-Patterns/dp/0201895277 Pattern Languages of Program Design 2].&lt;br /&gt;
&lt;br /&gt;
Currently there is little information available online. The best articles still available are:&lt;br /&gt;
&lt;br /&gt;
* [http://www.ksc.com/article1.htm Crossing Chasms: The Architectural Patterns]&lt;br /&gt;
* [http://www.ksc.com/article2.htm A Pattern Language for Relational Databases and Smalltalk]&lt;br /&gt;
* [http://www.ksc.com/article5.htm Crossing Chasms: A Pattern Language for Object-RDBMS Integration &amp;quot;The Static Patterns&amp;quot;]&lt;br /&gt;
* [http://www.smallmemory.com/almanac/BrownEtc96b.html Crossing Chasms: A Pattern Language for Object-RDBMS Integration]&lt;br /&gt;
* [http://c2.com/cgi/wiki?CrossingChasms Crossing Chasms]&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6448</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6448"/>
		<updated>2007-10-22T00:53:33Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems like the Active Record implementation in Ruby provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row (http://www.martinfowler.com/eaaCatalog/activeRecord.html):&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' &lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
The major features of the Rails Active Record implementation are:&lt;br /&gt;
&lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire life cycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire life cycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like Java Hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6444</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6444"/>
		<updated>2007-10-22T00:43:26Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* Ruby's Active Record */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems like the Active Record implementation in Ruby provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Active Record ==&lt;br /&gt;
&lt;br /&gt;
The Active Record pattern describes the most basic mapping of one object per table row (http://www.martinfowler.com/eaaCatalog/activeRecord.html):&lt;br /&gt;
&lt;br /&gt;
''An object that wraps a row in a database table or view, encapsulates the database access, and adds domain logic on that data.'' &lt;br /&gt;
&lt;br /&gt;
One of the best examples of the implementation of the pattern is found in the Rails framework (http://ar.rubyonrails.com/).&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6443</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6443"/>
		<updated>2007-10-22T00:35:28Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* How ORM works? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and vice versa. In static languages the mapping is done manually or it is automated with a preprocessor or with external mapping data (e.g. mapping files in Java Hibernate). In languages that support meta programming at runtime or compile time the mapping can be done automatically from information gathered from the database. ORM systems like the Active Record implementation in Ruby provide the programmer with object factories that map class method calls to SQL queries. They also provide methods to store the object data back in the database after updates in the application.&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Ruby's Active Record ==&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
== Apache ObJectRelationalBridge - OJB ==&lt;br /&gt;
&lt;br /&gt;
* The PersistenceBroker kernel API and all top-level APIs (ODMG, OTM, JDO) allows Java Programmers to store and retrieve Java Objects in/from (any) JDBC-compliant RDBMS&lt;br /&gt;
* Transparent persistence: classes does not have to inherit from any OJB base class nor implement a special interface. OJB delivers pure transparent persistence for POJOs.&lt;br /&gt;
* Scalable architecture that allows to build massively distributed and clustered systems.&lt;br /&gt;
* Quality assurance taken seriously: More than 800  JUnit Test Cases for regression tests. JUnit tests are integrated into the build scripts and used as quality assurance for daily development.&lt;br /&gt;
* Support for Polymorphism and Extents. You can use Interface-types and abstract classes as attribute types in your persistent classes. Queries are also aware of extents: A query against a baseclass or interface will return matches from derived classes, even if they are mapped to different DB-tables&lt;br /&gt;
* Support for persistent object caching. Different caching strategies and distributed caches.&lt;br /&gt;
* OJB uses an XML based Object/Relational mapping. The mapping resides in a dynamic MetaData layer, which can be manipulated at runtime through a simple Meta-Object-Protocol (MOP) to change the behaviour of the persistence kernel.&lt;br /&gt;
* OJB provides several advanced O/R features like Object Caching, lazy materialization through virtual proxies and distributed lock-management with configurable Transaction-Isolation levels. Optimistic and pessimistic locking is supported.&lt;br /&gt;
* OJB provides a flexible configuration and plugin mechanism that allows to select from set of predefined components or to implement your own extensions and plugins.&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6436</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6436"/>
		<updated>2007-10-22T00:12:42Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* What is ORM? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Ruby's Active Record ==&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6292</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6292"/>
		<updated>2007-10-21T14:48:10Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* What is ORM? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent data from the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and viceversa.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Ruby's Active Record ==&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6289</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6289"/>
		<updated>2007-10-21T14:45:25Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* What is ORM? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping (ORM) is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent representations of the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and viceversa.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Ruby's Active Record ==&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6287</id>
		<title>CSC/ECE 517 Fall 2007/wiki2 2 22</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki2_2_22&amp;diff=6287"/>
		<updated>2007-10-21T14:45:04Z</updated>

		<summary type="html">&lt;p&gt;Jvegafr: /* What is ORM? */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''Object-relational mapping. Ruby's ActiveRecord is one attempt to allow an object-oriented program to use a relational database. The Crossing Chasms pattern is another. Look up several approaches to mapping relational databases to o-o programs, include hyperlinks to all of them, and explain how they differ. Report on the strengths of various approaches (making sure to credit the authors for their insights).''&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
= Introduction =&lt;br /&gt;
&lt;br /&gt;
== What is ORM? ==&lt;br /&gt;
Object Relational Mapping pattern is a technique used in software systems that use an object-oriented language in their application domain and a relational database to store persistent representations of the objects they use. The mapping is required due to the differences in representation. The database uses tables and foreign keys for its representation of data and relationships where the application uses objects and references and/or pointers.&lt;br /&gt;
For some time proponents of Object-Oriented Database Management Systems (ODBMS) have argued that this is similar to breaking up a car to its pieces before storing it in the garage and putting it together again after taking it out. Whereas in an object database you store or retrieve the whole car at once.&lt;br /&gt;
Typical ORM systems will map one row in a table in the database to an object in the application and viceversa.&lt;br /&gt;
&lt;br /&gt;
== How ORM works? ==&lt;br /&gt;
&lt;br /&gt;
= ORM Implementations =&lt;br /&gt;
&lt;br /&gt;
== Ruby's Active Record ==&lt;br /&gt;
&lt;br /&gt;
Major features of Active Records &lt;br /&gt;
* Automated mapping between classes and tables, attributes and columns.&lt;br /&gt;
* Associations between objects controlled by simple meta-programming macros.&lt;br /&gt;
* Aggregations of value objects controlled by simple meta-programming macros.&lt;br /&gt;
* Validation rules that can differ for new or existing objects.&lt;br /&gt;
* Acts that can make records work as lists or trees&lt;br /&gt;
* Callbacks as methods or queues on the entire lifecycle (instantiation, saving, destroying, validating, etc).&lt;br /&gt;
* Observers for the entire lifecycle&lt;br /&gt;
* Inheritance hierarchies&lt;br /&gt;
* Transaction support on both a database and object level.&lt;br /&gt;
* Reflections on columns, associations, and aggregations&lt;br /&gt;
* Direct manipulation (instead of service invocation like hibernate)&lt;br /&gt;
* Database abstraction through simple adapters (~100 lines) with a shared connector&lt;br /&gt;
* Logging support for Log4r and Logger&lt;br /&gt;
&lt;br /&gt;
== Crossing Chasms pattern ==&lt;br /&gt;
&lt;br /&gt;
== Hibernate ==&lt;br /&gt;
&lt;br /&gt;
Hibernate supports key OO features&lt;br /&gt;
* Hibernate supports natural OO idiom; inheritance, polymorphism, composition and the Java collections framework&lt;br /&gt;
* It supports fine-grained object models - a rich variety of mappings for collections and dependent objects&lt;br /&gt;
* There is no extra code generation or bytecode processing steps in build procedure&lt;br /&gt;
* Hibernate is extremely performant, has a dual-layer cache architecture, and may be used in a cluster&lt;br /&gt;
* Hibernate addresses both sides of the problem; not only how to get objects into the database, but also how to get them out again&lt;br /&gt;
* Hibernate supports both long-lived persistence contexts, detach/reattach of objects, and takes care of optimistic locking automatically&lt;br /&gt;
* Hibernate implements the Java Persistence management API and object/relational mapping options (EJB 3.0), two members of the Hibernate team are active in the expert group&lt;br /&gt;
&lt;br /&gt;
== Oracle TopLink ==&lt;br /&gt;
&lt;br /&gt;
Oracle TopLink delivers complete Java Persistance platform by providing following main features:&lt;br /&gt;
* Access relational data using JPA and many advanced object-relational extensions&lt;br /&gt;
* Efficiently manipulate XML through a Java domain model using JAXB&lt;br /&gt;
* Interact with unstructured data through Service Data Objects (SDO)&lt;br /&gt;
* Expose relational datbases flexibly using Web Services&lt;br /&gt;
&lt;br /&gt;
== JDO ==&lt;br /&gt;
The Java Data Objects (JDO) API is a standard interface-based Java model abstraction of persistence,&lt;br /&gt;
* Application programmers can focus on their domain object model and leave the details of persistence (field-by-field storage of objects) to the JDO implementation. &lt;br /&gt;
* Applications written with the JDO API can be run on multiple implementations without recompiling or changing source code. Metadata, which describes persistence behavior external to the Java source code including most commonly used features of O/R mapping, is highly portable.&lt;br /&gt;
* Applications written with the JDO API are independent of the underlying database. JDO implementations support many different kinds of transactional data stores, including relational and object databases, XML, flat files, and others. &lt;br /&gt;
* Application programmers delegate the details of persistence to the JDO implementation, which can optimize data access patterns for optimal performance. &lt;br /&gt;
* Applications can take advantage of EJB features such as remote message processing, automatic distributed transaction coordination, and security, using the same domain object models throughout the enterprise.&lt;br /&gt;
&lt;br /&gt;
== EJB CMP Entity Beans ==&lt;br /&gt;
&lt;br /&gt;
= Comparison =&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
&lt;br /&gt;
= Further reading =&lt;br /&gt;
&lt;br /&gt;
= External links =&lt;/div&gt;</summary>
		<author><name>Jvegafr</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5493</id>
		<title>CSC/ECE 517 Fall 2007/wiki1b 8 sa</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1b_8_sa&amp;diff=5493"/>
		<updated>2007-10-11T01:15:05Z</updated>

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

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

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

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

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

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

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

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

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

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

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