<?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=Crazzyy</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=Crazzyy"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Crazzyy"/>
	<updated>2026-09-13T01:02:56Z</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_2009/wiki3_18_sd&amp;diff=28635</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 18 sd</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_18_sd&amp;diff=28635"/>
		<updated>2009-11-18T20:55:42Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/  Dependency Inversion Principle ]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==What does the Principle say?==&lt;br /&gt;
&lt;br /&gt;
After explaining the relationship between stability and dependencies, this seems to be an obvious principle. Essentially, if we want packages or classes to change, we want it to be easy, and thus we isolate the unstable packages or classes and make them independent. It helps to define dependency chain which helps to localize the changes but at the same time helps to keep the system flexible [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/].&lt;br /&gt;
&lt;br /&gt;
A simple way to arrange packages would be to classify them as stable(difficult to change) and volatile(easy to change). Stable packages must not depend on Volatile one because&lt;br /&gt;
&lt;br /&gt;
- It makes Volatile packages harder to change&lt;br /&gt;
&lt;br /&gt;
- Or It may force stable packages to be changed often, in other words volatile becomes stable&lt;br /&gt;
&lt;br /&gt;
Thus we have to ensure that volatile packages must depend on stable packages [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
Consider an example which would explain the above scenario. Lets consider an example where a Class library L depends on the collection class library C which further depends on special memory allocation library M. For execution speed, API of C is modeled after API of M. Now M has a serious bug which needs to be fixed. Thus Class C needs to be updated. How does this affect all the classes here?&lt;br /&gt;
&lt;br /&gt;
- Fixing issues in Class M causes API changes in C which requires changes in the header files of Class C&lt;br /&gt;
&lt;br /&gt;
- As API of M reflects in API of C, L also has to be modified too.&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Uncle Bob also defined stability metrics to the stability of the packages. These metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or number of classes or packages that depend upon classes within this package or Afferent Couplings &lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or number of classes inside this package that depend upon classes outside this package or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other outside packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other outside packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies [http://www.objectmentor.com/resources/articles/stability.pdf].&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;br /&gt;
&lt;br /&gt;
==Where do we put the high level design?==&lt;br /&gt;
We explained the metrics and coupling standards which an ideal system should follow. But after all this there will always remain some software which needs to be changed. These are the high level packages which we described in the sections above.  Thus, placing these high level packages are a problem. Should they be in stable packages? This would make the changes very difficult [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf]. &lt;br /&gt;
&lt;br /&gt;
So only way out would be to use [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle]. According to this principle, it is possible to create classes that are flexible enough to be extended without modification. Abstract Classes are kind of classes  in which specifications can be reused through inheritance but there is no compulsion on its implementation.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
# [http://today.java.net/pub/au/90 Robert C. Martin]&lt;br /&gt;
#[http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf Design Principles and Design Patterns by Robert C. Martin]&lt;br /&gt;
# [http://www.objectmentor.com/resources/articles/oodmetrc.pdf OO Design Quality Metrics by Robert Martin]&lt;br /&gt;
# [http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle]&lt;br /&gt;
# [http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle]&lt;br /&gt;
# [http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle]&lt;br /&gt;
#[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/ Dependency Inversion Principle ]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/ Blog Stable Dependencies Principle]&lt;br /&gt;
# [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf Object Oriented Design in MultiCore Systems]&lt;br /&gt;
# [http://www.objectmentor.com/resources/articles/stability.pdf Stability by Robert Martin]&lt;br /&gt;
# [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf Package Design]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28625</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28625"/>
		<updated>2009-11-18T20:51:19Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/  Dependency Inversion Principle ]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==What does the Principle say?==&lt;br /&gt;
&lt;br /&gt;
After explaining the relationship between stability and dependencies, this seems to be an obvious principle. Essentially, if we want packages or classes to change, we want it to be easy, and thus we isolate the unstable packages or classes and make them independent. It helps to define dependency chain which helps to localize the changes but at the same time helps to keep the system flexible [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/].&lt;br /&gt;
&lt;br /&gt;
A simple way to arrange packages would be to classify them as stable(difficult to change) and volatile(easy to change). Stable packages must not depend on Volatile one because&lt;br /&gt;
&lt;br /&gt;
- It makes Volatile packages harder to change&lt;br /&gt;
&lt;br /&gt;
- Or It may force stable packages to be changed often, in other words volatile becomes stable&lt;br /&gt;
&lt;br /&gt;
Thus we have to ensure that volatile packages must depend on stable packages [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
Consider an example which would explain the above scenario. Lets consider an example where a Class library L depends on the collection class library C which further depends on special memory allocation library M. For execution speed, API of C is modeled after API of M. Now M has a serious bug which needs to be fixed. Thus Class C needs to be updated. How does this affect all the classes here?&lt;br /&gt;
&lt;br /&gt;
- Fixing issues in Class M causes API changes in C which requires changes in the header files of Class C&lt;br /&gt;
&lt;br /&gt;
- As API of M reflects in API of C, L also has to be modified too.&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Uncle Bob also defined stability metrics to the stability of the packages. These metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or number of classes or packages that depend upon classes within this package or Afferent Couplings &lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or number of classes inside this package that depend upon classes outside this package or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other outside packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other outside packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies [http://www.objectmentor.com/resources/articles/stability.pdf].&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;br /&gt;
&lt;br /&gt;
==Where do we put the high level design?==&lt;br /&gt;
We explained the metrics and coupling standards which an ideal system should follow. But after all this there will always remain some software which needs to be changed. These are the high level packages which we described in the sections above.  Thus, placing these high level packages are a problem. Should they be in stable packages? This would make the changes very difficult [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf]. &lt;br /&gt;
&lt;br /&gt;
So only way out would be to use [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle]. According to this principle, it is possible to create classes that are flexible enough to be extended without modification. Abstract Classes are kind of classes  in which specifications can be reused through inheritance but there is no compulsion on its implementation.&lt;br /&gt;
&lt;br /&gt;
= References =&lt;br /&gt;
# [http://today.java.net/pub/au/90 Robert C. Martin]&lt;br /&gt;
#[http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf Design Principles and Design Patterns by Robert C. Martin]&lt;br /&gt;
# [http://www.objectmentor.com/resources/articles/oodmetrc.pdf OO Design Quality Metrics by Robert Martin]&lt;br /&gt;
# [http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle]&lt;br /&gt;
# [http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle]&lt;br /&gt;
# [http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle]&lt;br /&gt;
#[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle]&lt;br /&gt;
# [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/ Dependency Inversion Principle ]&lt;br /&gt;
# [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/ Blog Stable Dependencies Principle]&lt;br /&gt;
# [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf Object Oriented Design in MultiCore Systems]&lt;br /&gt;
# [http://www.objectmentor.com/resources/articles/stability.pdf Stability by Robert Martin]&lt;br /&gt;
# [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf Package Design]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28614</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28614"/>
		<updated>2009-11-18T20:39:30Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the Dependency Inversion Principle [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==What does the Principle say?==&lt;br /&gt;
&lt;br /&gt;
After explaining the relationship between stability and dependencies, this seems to be an obvious principle. Essentially, if we want packages or classes to change, we want it to be easy, and thus we isolate the unstable packages or classes and make them independent. It helps to define dependency chain which helps to localize the changes but at the same time helps to keep the system flexible [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/].&lt;br /&gt;
&lt;br /&gt;
A simple way to arrange packages would be to classify them as stable(difficult to change) and volatile(easy to change). Stable packages must not depend on Volatile one because&lt;br /&gt;
&lt;br /&gt;
- It makes Volatile packages harder to change&lt;br /&gt;
&lt;br /&gt;
- Or It may force stable packages to be changed often, in other words volatile becomes stable&lt;br /&gt;
&lt;br /&gt;
Thus we have to ensure that volatile packages must depend on stable packages [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
Consider an example which would explain the above scenario. Lets consider an example where a Class library L depends on the collection class library C which further depends on special memory allocation library M. For execution speed, API of C is modeled after API of M. Now M has a serious bug which needs to be fixed. Thus Class C needs to be updated. How does this affect all the classes here?&lt;br /&gt;
&lt;br /&gt;
- Fixing issues in Class M causes API changes in C which requires changes in the header files of Class C&lt;br /&gt;
&lt;br /&gt;
- As API of M reflects in API of C, L also has to be modified too.&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Uncle Bob also defined stability metrics to the stability of the packages. These metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or number of classes or packages that depend upon classes within this package or Afferent Couplings &lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or number of classes inside this package that depend upon classes outside this package or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other outside packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other outside packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies [http://www.objectmentor.com/resources/articles/stability.pdf].&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;br /&gt;
&lt;br /&gt;
==Where do we put the high level design?==&lt;br /&gt;
We explained the metrics and coupling standards which an ideal system should follow. But after all this there will always remain some software which needs to be changed. These are the high level packages which we described in the sections above.  Thus, placing these high level packages are a problem. Should they be in stable packages? This would make the changes very difficult [http://staff.cs.utu.fi/kurssit/Programming-III/PackageDesing.pdf]. &lt;br /&gt;
&lt;br /&gt;
So only way out would be to use [http://en.wikipedia.org/wiki/Open/closed_principle Open/Closed principle]. According to this principle, it is possible to create classes that are flexible enough to be extended without modification. Abstract Classes are kind of classes  in which specifications can be reused through inheritance but there is no compulsion on its implementation.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28581</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28581"/>
		<updated>2009-11-18T20:20:26Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the Dependency Inversion Principle [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==What does the Principle say?==&lt;br /&gt;
&lt;br /&gt;
After explaining the relationship between stability and dependencies, this seems to be an obvious principle. Essentially, if we want packages or classes to change, we want it to be easy, and thus we isolate the unstable packages or classes and make them independent. It helps to define dependency chain which helps to localize the changes but at the same time helps to keep the system flexible [http://ifacethoughts.net/2006/04/15/stable-dependencies-principle/].&lt;br /&gt;
&lt;br /&gt;
A simple way to arrange packages would be to classify them as stable(difficult to change) and volatile(easy to change). Stable packages must not depend on Volatile one because&lt;br /&gt;
&lt;br /&gt;
- It makes Volatile packages harder to change&lt;br /&gt;
&lt;br /&gt;
- Or It may force stable packages to be changed often, in other words volatile becomes stable&lt;br /&gt;
&lt;br /&gt;
Thus we have to ensure that volatile packages must depend on stable packages [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
Consider an example which would explain the above scenario. Lets consider an example where a Class library L depends on the collection class library C which further depends on special memory allocation library M. For execution speed, API of C is modeled after API of M. Now M has a serious bug which needs to be fixed. Thus Class C needs to be updated. How does this affect all the classes here?&lt;br /&gt;
&lt;br /&gt;
- Fixing issues in Class M causes API changes in C which requires changes in the header files of Class C&lt;br /&gt;
&lt;br /&gt;
- As API of M reflects in API of C, L also has to be modified too.&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Uncle Bob also defined stability metrics to the stability of the packages. These metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package [http://eple.hib.no/irtg/talks/IRTG-Meeting-200902-Lecture-OO-Design.pdf].&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or number of classes or packages that depend upon classes within this package or Afferent Couplings &lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or number of classes inside this package that depend upon classes outside this package or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other outside packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other outside packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies [http://www.objectmentor.com/resources/articles/stability.pdf].&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28554</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28554"/>
		<updated>2009-11-18T20:07:24Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the Dependency Inversion Principle [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==What does the Principle say?==&lt;br /&gt;
&lt;br /&gt;
After explaining the relationship between stability and dependencies, this seems to be an obvious principle. Essentially, if we want packages or classes to change, we want it to be easy, and thus we isolate the unstable packages or classes and make them independent. It helps to define dependency chain which helps to localize the changes but at the same time helps to keep the system flexible.&lt;br /&gt;
&lt;br /&gt;
A simple way to arrange packages would be to classify them as stable(difficult to change) and volatile(easy to change). Stable packages must not depend on Volatile one because&lt;br /&gt;
&lt;br /&gt;
- It makes Volatile packages harder to change&lt;br /&gt;
&lt;br /&gt;
- Or It may force stable packages to be changed often, in other words volatile becomes stable&lt;br /&gt;
&lt;br /&gt;
Thus we have to ensure that volatile packages must depend on stable packages.&lt;br /&gt;
&lt;br /&gt;
Consider an example which would explain the above scenario. Lets consider an example where a Class library L depends on the collection class library C which further depends on special memory allocation library M. For execution speed, API of C is modeled after API of M. Now M has a serious bug which needs to be fixed. Thus Class C needs to be updated. How does this affect all the classes here?&lt;br /&gt;
&lt;br /&gt;
- Fixing issues in Class M causes API changes in C which requires changes in the header files of Class C&lt;br /&gt;
&lt;br /&gt;
- As API of M reflects in API of C, L also has to be modified too.&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Stability metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package.&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or Afferent Couplings&lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies.&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28537</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28537"/>
		<updated>2009-11-18T19:54:07Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the Dependency Inversion Principle [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms like stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Stability metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package.&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or Afferent Couplings&lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies.&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28533</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=28533"/>
		<updated>2009-11-18T19:51:33Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Stable Dependencies Principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is a very simple and atomic principle. It is a direct derivation of the Dependency Inversion Principle [http://ifacethoughts.net/2006/03/16/dependency-inversion-principle-and-interface/]. This principle enables high reusability and easier maintenance of software applications. To explain the principle further we need to understand a few terms line stability, dependencies etc in more detail.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it.&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability Metrics==&lt;br /&gt;
Stability metrics determine the stability of a package which is calculated by counting the number of dependencies that enter and leave the package.&lt;br /&gt;
&lt;br /&gt;
The Instability metric I is given as &lt;br /&gt;
&lt;br /&gt;
'''I = Ca/(Ca +Ce)'''&lt;br /&gt;
&lt;br /&gt;
where,&lt;br /&gt;
&lt;br /&gt;
Ca --&amp;gt; The number of incoming dependencies or Afferent Couplings&lt;br /&gt;
   &lt;br /&gt;
Ce --&amp;gt; The number of outgoing dependencies or Efferent Couplings&lt;br /&gt;
&lt;br /&gt;
The value of I is always between 0 and 1.&lt;br /&gt;
&lt;br /&gt;
- '''I = 0''' is a Stable Package. In this case, other packages have a dependency on this package but it does not depend on any other packages, hence ''responsible and independent''.&lt;br /&gt;
&lt;br /&gt;
- '''I = 1''' is an Instable Package. Here, no other packages have a dependency on this package but this package depends on the other packages, hence ''irresponsible and dependent''.&lt;br /&gt;
&lt;br /&gt;
The values Ca and Ce are calculated by counting the number of classes outside the package that have dependencies with the classes inside the package being considered. A figure explaining how the metric can be calculated is shown below&lt;br /&gt;
&lt;br /&gt;
[[Image:metrics.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
In the above example we can compute the Instability metric of the package in the center. Here the dashed arrows represent package dependencies.&lt;br /&gt;
&lt;br /&gt;
Here Ca, which is the number of classes outside the package at center that depend upon the classes within this package is equal to 4. Whereas Ce, which is the number of classes inside the package at center that depend upon classes outside this package is 3.&lt;br /&gt;
Hence I = 3/7.&lt;br /&gt;
&lt;br /&gt;
Thus according to the Stable Dependencies Principle I metric of the package should be greater than the I metric of the packagesit depends upon thereby showing that I metrics decrease in the direction of dependency.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
[[Image:2-25.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
[[Image:2-26.jpg|centre]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26935</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26935"/>
		<updated>2009-11-15T18:49:46Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[have to exagerate here with respect to basic definition]. To explain the principle in detail, we need to first understand few terms like stability, dependencies etc first.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it.&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
With the above definition of stability, you don't want to increase stability of the packages as your software can not change easily then. But if the system has been designed well then hard to change packages or stable packages will not change that frequently. Thus, we need to understand the relationship between stability and dependencies. &lt;br /&gt;
&lt;br /&gt;
Consider a following example, Here package x, depends on three other packages. Thus, it is said that x is responsible to the other three packages. On other hand, x does not depend on anything, thus it does not influence any other change. It is said to be independent [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf].&lt;br /&gt;
&lt;br /&gt;
(Could you put an image here figure 2-25 on page 23)&lt;br /&gt;
&lt;br /&gt;
Another example, here package y has no packages depending upon it, thus we call it irresponsible. It is very instable package. But there are three other packages that y depends upon, so changes from these three external packages may affect it and thus we say y is dependent.&lt;br /&gt;
&lt;br /&gt;
(Could you put an image here figure 2-26 on page 23)&lt;br /&gt;
&lt;br /&gt;
Thus we need to be careful while designing packages especially lower level packages (which would have more users) and higher level packages (which would tend to have fewer users).&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26934</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26934"/>
		<updated>2009-11-15T18:21:33Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[have to exagerate here with respect to basic definition]. To explain the principle in detail, we need to first understand few terms like stability, dependencies etc first.&lt;br /&gt;
&lt;br /&gt;
==Stability and Dependencies==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it.&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26933</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26933"/>
		<updated>2009-11-15T18:20:28Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Stable Dependencies Principle */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To explain the principle in detail, we need to first understand few terms like stability, dependencies etc first.&lt;br /&gt;
&lt;br /&gt;
==Stability==&lt;br /&gt;
What does stability mean? By simply goggling stability, we get definitions like resistance to change, reliability etc [http://www.answers.com/topic/stability]. Thus it roughly means &amp;quot;hard to change&amp;quot;, whereas instability means &amp;quot;easy to change&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
Consider an example, in which you stand a penny on its side. Is this penny stable in that position? Mostly people would answer a no, even though it does not change its position for a very long time. Thus, stability has nothing directly to do with the frequency of change. It more relates to the amount of work required to make a change. The penny is not stable because it takes little work to topple it.&lt;br /&gt;
&lt;br /&gt;
Thus stability of a software package would depend on various factors like size, complexity, clarity, etc.  But with respect to our principle, stability focus on a different factor. A software package would be difficult to change, if it has lots of other software packages depend on upon it. Thus a package with lots of incoming dependencies is very stable because it requires a great deal of work for any change to propagate with all the dependent packages.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26929</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26929"/>
		<updated>2009-11-15T17:43:14Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Bob Martin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above can be caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26922</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26922"/>
		<updated>2009-11-15T17:26:46Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Bob Martin */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Martin] (Uncle Bob) defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above are caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26921</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26921"/>
		<updated>2009-11-15T17:25:29Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above are caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26920</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26920"/>
		<updated>2009-11-15T17:25:06Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
===Bob Martin===&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above are caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
===Stable Dependencies Principle===&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26918</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26918"/>
		<updated>2009-11-15T17:24:22Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==Bob Martin==&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above are caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
==Stable Dependencies Principle==&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26916</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26916"/>
		<updated>2009-11-15T17:23:38Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle. Each of the four problems discussed above are caused by improper dependencies between the modules of the software. It is the dependency architecture that degrades the ability of the software to be maintained. The Stable Dependencies Principle is one of the principle which discusses its concern with coupling of packages, or the degree to which they rely on each other.&lt;br /&gt;
&lt;br /&gt;
=Stable Dependencies Principle=&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
Principle:&lt;br /&gt;
''The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.''&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26914</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26914"/>
		<updated>2009-11-15T17:13:35Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle],&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle],&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle],&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle],&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle].&lt;br /&gt;
&lt;br /&gt;
The objective of this article is to discuss in detail Stable Dependencies Principle.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26912</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26912"/>
		<updated>2009-11-15T17:09:19Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=Bob Martin=&lt;br /&gt;
&lt;br /&gt;
[http://today.java.net/pub/au/90 Robert C. Miller] defines the domain of [http://www.objectmentor.com/resources/articles/Principles_and_Patterns.pdf design patterns] to be the architecture of systems modules (packages, classes and components) and their interconnections.  His design patterns address the four major reasons for failure of the systems:  rigidity, fragility, immobility and viscosity [http://www.objectmentor.com/resources/articles/oodmetrc.pdf]. &lt;br /&gt;
&lt;br /&gt;
Following are the list of few design principles quoted by him to avoid the pitfalls associated with unsuccessful software engineering,&lt;br /&gt;
[http://labs.cs.utt.ro/labs/ip2/html/lectures/5/lecture5.ppt Reuse/Release Equivalence Principle]&lt;br /&gt;
[http://iface.wordpress.com/2006/04/08/common-closure-principle/ Common Closure Principle]&lt;br /&gt;
[http://www.objectmentor.com/omSolutions/oops_what.html Common Reuse Principle]&lt;br /&gt;
[http://ifacethoughts.net/2006/04/10/acyclic-dependencies-principle/ Acyclic Dependencies Principle]&lt;br /&gt;
[http://www.objectmentor.com/resources/articles/stability.pdf Staple Dependencies Principle]&lt;br /&gt;
[http://ifacethoughts.net/2006/04/20/stable-abstractions-principle/ Stable Abstraction Principle]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26908</id>
		<title>CSC/ECE 517 Fall 2009/wiki1b 13 zz</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1b_13_zz&amp;diff=26908"/>
		<updated>2009-11-15T16:54:45Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Bob Martin's Stable Dependencies Principle says, &amp;quot;The dependencies between packages should be in the direction of the stability of the packages. A package should only depend upon packages that are more stable than it is.&amp;quot;&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25618</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25618"/>
		<updated>2009-10-11T01:14:17Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.''&lt;br /&gt;
&lt;br /&gt;
=Introduction= &lt;br /&gt;
&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts [http://en.wikipedia.org/wiki/Creational_pattern Creational], [http://en.wikipedia.org/wiki/Structural_pattern Structural], [http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral] [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory Design Pattern=&lt;br /&gt;
==What is Factory Design Pattern?==&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate the code that creates the class form the concrete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
The given code explains how to create a factory in Ruby. In this it initially creates a factory class called GearFactory and overrides the new function. When an object is instantiated the code does not need to know which kind of object it is. It is a collection (hence the word factory) of objects that are clubbed together.&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the following code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Both the languages implement the factory design pattern. Java implements the factory design pattern using the interfaces while Ruby uses classes to create a factory. An abstract base class can be defined in Java to hold all the interfaces needed or there could be a parameterised method which takes the kind of object as a parameter to instantiate the necessary object. &lt;br /&gt;
In Ruby 'new' is just a method on a class object, it's always free to return anything it likes.  The nice thing about this is that, in ruby, every call to new is, by definition, the factory pattern - it just so happens that there is a default implementation inherited by class 'Class'.&lt;br /&gt;
The factory implementation of Ruby is a lot simpler than the factory implementation of Java.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf Design Patterns]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/JavaPatterns.htm Design Patterns Java Companion James W Cooper]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Wiki Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract Factory Method]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/abstract_factory SourceMaking explains Abstract Factory]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf Abstract Factory Example in Java]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternAbstract.aspx Abstract Factory explained]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Iterator_pattern Iterator Pattern]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/iterator Sourcemaking explains Iterator method]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf Iterator method explained]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternIterator.aspx DoFactory explains Iterator pattern]&lt;br /&gt;
#[http://us3.php.net/manual/en/language.oop5.iterations.php Iterator pattern for PHP]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/Decorator.pdf Decorator Pattern]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern Wiki Decorator Pattern]&lt;br /&gt;
#[http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby Design Patterns in Ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript Design Patterns Javascript]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25616</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25616"/>
		<updated>2009-10-11T01:02:30Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
change&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts [http://en.wikipedia.org/wiki/Creational_pattern Creational], [http://en.wikipedia.org/wiki/Structural_pattern Structural], [http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral] [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory Design Pattern=&lt;br /&gt;
==What is Factory Design Pattern?==&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate the code that creates the class form the concrete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
The given code explains how to create a factory in Ruby. In this it initially creates a factory class called GearFactory and overrides the new function. When an object is instantiated the code does not need to know which kind of object it is. It is a collection (hence the word factory) of objects that are clubbed together.&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the following code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Both the languages implement the factory design pattern. Java implements the factory design pattern using the interfaces while Ruby uses classes to create a factory. An abstract base class can be defined in Java to hold all the interfaces needed or there could be a parameterised method which takes the kind of object as a parameter to instantiate the necessary object. &lt;br /&gt;
In Ruby 'new' is just a method on a class object, it's always free to return anything it likes.  The nice thing about this is that, in ruby, every call to new is, by definition, the factory pattern - it just so happens that there is a default implementation inherited by class 'Class'.&lt;br /&gt;
The factory implementation of Ruby is a lot simpler than the factory implementation of Java.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf Design Patterns]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/JavaPatterns.htm Design Patterns Java Companion James W Cooper]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Wiki Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract Factory Method]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/abstract_factory SourceMaking explains Abstract Factory]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf Abstract Factory Example in Java]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternAbstract.aspx Abstract Factory explained]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Iterator_pattern Iterator Pattern]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/iterator Sourcemaking explains Iterator method]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf Iterator method explained]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternIterator.aspx DoFactory explains Iterator pattern]&lt;br /&gt;
#[http://us3.php.net/manual/en/language.oop5.iterations.php Iterator pattern for PHP]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/Decorator.pdf Decorator Pattern]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern Wiki Decorator Pattern]&lt;br /&gt;
#[http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby Design Patterns in Ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript Design Patterns Javascript]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25614</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=25614"/>
		<updated>2009-10-11T00:56:46Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
For change&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts [http://en.wikipedia.org/wiki/Creational_pattern Creational], [http://en.wikipedia.org/wiki/Structural_pattern Structural], [http://en.wikipedia.org/wiki/Behavioral_pattern Behavioral] [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory Design Pattern=&lt;br /&gt;
==What is Factory Design Pattern?==&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate the code that creates the class form the concrete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
The given code explains how to create a factory in Ruby. In this it initially creates a factory class called GearFactory and overrides the new function. When an object is instantiated the code does not need to know which kind of object it is. It is a collection (hence the word factory) of objects that are clubbed together.&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the following code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Both the languages implement the factory design pattern. Java implements the factory design pattern using the interfaces while Ruby uses classes to create a factory. An abstract base class can be defined in Java to hold all the interfaces needed or there could be a parameterised method which takes the kind of object as a parameter to instantiate the necessary object. &lt;br /&gt;
In Ruby 'new' is just a method on a class object, it's always free to return anything it likes.  The nice thing about this is that, in ruby, every call to new is, by definition, the factory pattern - it just so happens that there is a default implementation inherited by class 'Class'.&lt;br /&gt;
The factory implementation of Ruby is a lot simpler than the factory implementation of Java.&lt;br /&gt;
&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;br /&gt;
&lt;br /&gt;
=References=&lt;br /&gt;
#[http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf Design Patterns]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/JavaPatterns.htm Design Patterns Java Companion James W Cooper]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29 Wiki Design Patterns]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Abstract_factory_pattern Abstract Factory Method]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/abstract_factory SourceMaking explains Abstract Factory]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf Abstract Factory Example in Java]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternAbstract.aspx Abstract Factory explained]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Iterator_pattern Iterator Pattern]&lt;br /&gt;
#[http://sourcemaking.com/design_patterns/iterator Sourcemaking explains Iterator method]&lt;br /&gt;
#[http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf Iterator method explained]&lt;br /&gt;
#[http://www.dofactory.com/Patterns/PatternIterator.aspx DoFactory explains Iterator pattern]&lt;br /&gt;
#[http://us3.php.net/manual/en/language.oop5.iterations.php Iterator pattern for PHP]&lt;br /&gt;
#[http://www.patterndepot.com/put/8/Decorator.pdf Decorator Pattern]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern Wiki Decorator Pattern]&lt;br /&gt;
#[http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby Design Patterns in Ruby]&lt;br /&gt;
#[http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript Design Patterns Javascript]&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23240</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23240"/>
		<updated>2009-10-08T23:50:05Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;''What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.''&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts Creational, Structural, Behavioral [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23237</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23237"/>
		<updated>2009-10-08T23:49:39Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;What facilities does Ruby offer that makes it easier to realize other GoF (and other) patterns that we did not cover in class? The key idea here is to explore how Ruby can implement these patterns more efficiently or transparently than static (or other dynamic) o-o languages.&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts Creational, Structural, Behavioral [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23233</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23233"/>
		<updated>2009-10-08T23:48:15Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Facilities Ruby offers to realize Design Patterns&lt;br /&gt;
&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
&amp;quot;The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past&amp;quot; [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem. Design patterns can be classified into 3 parts Creational, Structural, Behavioral [http://www.patterndepot.com/put/8/JavaPatterns.htm].&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23227</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23227"/>
		<updated>2009-10-08T23:42:17Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Decorator Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;br /&gt;
&lt;br /&gt;
=Conclusion=&lt;br /&gt;
&lt;br /&gt;
We have seen four design patterns Factory design pattern, Abstract Factory design pattern, Iterator design pattern and Decorator Design Pattern. We have chosen these patterns as they are widely used and Ruby has in-built implementations of these. We have mainly tried to compare Ruby and Java but also specified tips about other languages like PHP, C# and Javascript. In Ruby implementing these design patterns is simple and easy because of its main feature of private class objects, unbounded polymorphism and duck typing [http://people.engr.ncsu.edu/efg/517/f07/lectures/notes/lec6.pdf]. In Java all the features are mainly implemented through interfaces and classes. This has several disadvantages like increase in code complexity, size and is generally confusing and tedious to trace or understand the code.&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23221</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23221"/>
		<updated>2009-10-08T23:31:03Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Decorator Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
&lt;br /&gt;
This pattern is used to increase the functionality of the existing object dynamically. Suppose we have a program that uses eight objects, but three of them need an additional feature. We could create derived class for these objects having the additional features but then we can create a Decorator class which will add any specific kind of feature required [http://www.patterndepot.com/put/8/Decorator.pdf]. It is a type of behavioral pattern.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
&lt;br /&gt;
Generic decorators can be implmented by using the method_missing method. This method is called when an object receives a message that it does not have a method fo. The method_missing method can forward the message on to other object and wrap additional behavior around the call. In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9. The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. The need for this kind of pattern is so as to increase the functionality of a particular class [http://en.wikipedia.org/wiki/Decorator_pattern]. &lt;br /&gt;
&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
There are various other options to implement this design pattern. Other way to implememt the pattern is by using singleton methods and method aliases. Delegation can also be used, and it overcomes one of the most important disadvantage. We can remove add features if we are using delegation but this is not possible if we are using singleton methods and method aliases. Example for this can be found here [http://www.scribd.com/doc/2217773/Design-Patterns-in-Ruby].&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, this pattern is easy to implement by dividing the various responsibilities into classes and interfaces. The below example consider all these responsibilities and have four components which are described below  &lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Comparison of implementations==&lt;br /&gt;
&lt;br /&gt;
Ruby provides different options to implement Decorator pattern like method_missing, delegation and alias methods. This provides flexibility to the user to implement any possible way depending on the requirements and features needed. But in Java, there is more clearer approach using the components provided in the example. It is a straight forward approach. But different options provided by Ruby make it much better than Java.&lt;br /&gt;
&lt;br /&gt;
In dynamic languages like Javascript, decorator pattern can be implemented with no interfaces or traditional OOP inheritance. With simple overriding but this approach is difficult to implement [http://en.wikipedia.org/wiki/Decorator_pattern#JavaScript].&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23048</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23048"/>
		<updated>2009-10-08T19:17:01Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Iterator Design Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
==What is Iterator Design Pattern?==&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23047</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23047"/>
		<updated>2009-10-08T19:16:23Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Abstract Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
=Abstract Factory Pattern=&lt;br /&gt;
==What is Abstract Factory Pattern?==&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
==Implementation in Ruby==&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
==Implementation in Java==&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
==Comparison of Implementations==&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23046</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23046"/>
		<updated>2009-10-08T19:15:18Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern [http://www.dofactory.com/Patterns/PatternIterator.aspx]. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement [http://us3.php.net/manual/en/language.oop5.iterations.php].&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23043</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23043"/>
		<updated>2009-10-08T19:14:30Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. It hides the structure and there is no need for having implementation for those methods in the class. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23038</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23038"/>
		<updated>2009-10-08T19:12:55Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Iterator Design Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
&lt;br /&gt;
Overall implementation of Iterator design pattern in each object oriented language is simple as it is a basis feature of objects. But in general, the Ruby implementation is more concise. Ruby has built-in iterators which make it easier to implement Iterator pattern for any kind of object. But in Java, the built-in library function help to create easier and clear implementations. C# also has similar implementation as Java for this design pattern. &lt;br /&gt;
&lt;br /&gt;
In PHP, using an object in a foreach structure will traverse the public values. There are also many multiple Iterator classes available to allow us to iterate through common lists, such as directories, XML structures and recursive arrays. We can also implement our own interfaces depending on requirement.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23030</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23030"/>
		<updated>2009-10-08T18:59:53Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Iterator Design Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
&lt;br /&gt;
In Java, we can implement Iterator design pattern by using java.util.Enumeration interface which returns a reference to an object. Furthermore, Hashes and Vector have limited capabilities which help simple traversing. Java JDK 1.2 introduced a new Collections package with more aggregate classes, including sets, lists, maps and an Iterator interface. If we wanted to start implementing Iterator design pattern from start, then it would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf]. For example, consider the following scenario where iterator.First() and iterator.Next(), has been implemented in the class ListIterator.&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
    List list = new List();&lt;br /&gt;
    ...&lt;br /&gt;
    ListIterator iterator = new ListIterator(list);&lt;br /&gt;
    iterator.First();&lt;br /&gt;
    while (!iterator.IsDone()) {&lt;br /&gt;
    Object item = iterator.CurrentItem();&lt;br /&gt;
    // Code here to process item.&lt;br /&gt;
    iterator.Next();&lt;br /&gt;
    }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementation====&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23017</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23017"/>
		<updated>2009-10-08T18:37:54Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Iterator Design Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
==Iterator Design Pattern==&lt;br /&gt;
===What is Iterator Design Pattern?===&lt;br /&gt;
The Iterator design pattern allows an object to encapsulate the internal structure and allows the user to move through the collection of data using standard interface. It is one of the simplest and most frequently used design pattern. It is a type of Behavioral design pattern. [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
In Java implementing Iterator design pattern would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class. [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf] Thus Ruby helps in implementing this design pattern easily by providing functions like “each” which does the handling of the concrete class implementation.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23015</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23015"/>
		<updated>2009-10-08T18:32:33Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Implementation in Java */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
An Iterator object encapsulates the internal structure of how the iteration occurs. It is a type of Behavioral design pattern. [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
In Java implementing Iterator design pattern would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class. [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf] Thus Ruby helps in implementing this design pattern easily by providing functions like “each” which does the handling of the concrete class implementation.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23014</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23014"/>
		<updated>2009-10-08T18:32:03Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Abstract Factory Pattern */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   public class FactoryFmProto {&lt;br /&gt;
&lt;br /&gt;
  static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
   static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
   public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
  }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. Ruby automatically implements the Abstract Factory pattern as Class Objects. But Java needs helps of interfaces and classes to do so. Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
Similarly, C# demonstrates the Abstract Factory pattern by creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code. This property helps C# to have better implementation than Java. In .NET, there are built in features such as, generics, reflection, object initializers, automatic properties, etc for implementation [http://www.dofactory.com/Patterns/PatternAbstract.aspx].&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
An Iterator object encapsulates the internal structure of how the iteration occurs. It is a type of Behavioral design pattern. [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
In Java implementing Iterator design pattern would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class. [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf] Thus Ruby helps in implementing this design pattern easily by providing functions like “each” which does the handling of the concrete class implementation.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23012</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=23012"/>
		<updated>2009-10-08T18:22:37Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Comparison of Implementations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class FactoryFmProto {&lt;br /&gt;
&lt;br /&gt;
static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Basically, Java and Ruby, both can implement Abstract Factory design pattern. But the more important point is the simplicity by which they can apply it. &lt;br /&gt;
Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
An Iterator object encapsulates the internal structure of how the iteration occurs. It is a type of Behavioral design pattern. [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
In Java implementing Iterator design pattern would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class. [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf] Thus Ruby helps in implementing this design pattern easily by providing functions like “each” which does the handling of the concrete class implementation.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22877</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22877"/>
		<updated>2009-10-08T03:58:48Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* AbstractFactory Model */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Ruby==&lt;br /&gt;
&lt;br /&gt;
    class GearFactory&lt;br /&gt;
      def new() &lt;br /&gt;
        if ( ... some condition )&lt;br /&gt;
           return Sprocket.new()&lt;br /&gt;
        else&lt;br /&gt;
           return Cog().new()&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
    class GearUser &lt;br /&gt;
      def doSomething(factory )&lt;br /&gt;
        ...&lt;br /&gt;
      my_gear = factory.new()&lt;br /&gt;
        ...&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
    client.doSomething(GearFactory.new)          #Use the factory&lt;br /&gt;
    client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
    client.doSomething(Sprocket)                 #Use the Sprocket class&lt;br /&gt;
&lt;br /&gt;
==Factory Example in Java==&lt;br /&gt;
In java the Factory implementation is done using interfaces. Declaring them as interfaces helps to maintain a general overview and not depending on the type of factory object that needs to be used. All of these can be placed in a huge factory in a client application.&lt;br /&gt;
A well known example for Java Factory is the UI toolkits that are designed to run on different windowing systems.&lt;br /&gt;
    interface ScrollBar { ... }&lt;br /&gt;
    interface MenuBar   { ... }&lt;br /&gt;
    ...&lt;br /&gt;
And associated classes implementing them on different windowing systems:&lt;br /&gt;
&lt;br /&gt;
    class MotifScrollBar implements ScrollBar { ... }&lt;br /&gt;
    class Win95ScrollBar implements ScrollBar { ... }&lt;br /&gt;
    ...&lt;br /&gt;
&lt;br /&gt;
And a factory interface that also doesn't commit to representation:&lt;br /&gt;
&lt;br /&gt;
    interface Factory {&lt;br /&gt;
      public abstract ScrollBar newScrollBar();&lt;br /&gt;
      public abstract MenuBar   newMenuBar();&lt;br /&gt;
      ... &lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
But implementation classes that do:&lt;br /&gt;
&lt;br /&gt;
    class MotifFactory implements Factory {&lt;br /&gt;
      public ScrollBar newScrollBar() { return new MotifScrollBar(...); }&lt;br /&gt;
      ...&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
==Abstract Factory Pattern==&lt;br /&gt;
===What is Abstract Factory Pattern?===&lt;br /&gt;
Abstract Factory Design Pattern encapsulates a group of objects that have common theme. It implements a generic interface to create these objects that are part of the theme. It does not care about the details of the implementation of these objects. The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition. It is a type of Creational pattern [http://en.wikipedia.org/wiki/Abstract_factory_pattern] “Provide an interface for creating families of related or dependent objects without specifying their concrete classes” [http://sourcemaking.com/design_patterns/abstract_factory]&lt;br /&gt;
&lt;br /&gt;
===Implementation in Ruby===&lt;br /&gt;
Ruby automatically implements the Abstract Factory pattern as Class Objects. All Class objects have the same interface: the new method of each class object creates new instances of the class. Thus the code can pass references to class objects around and they can be used to call new method without knowing the exact type of object that the class creates.&lt;br /&gt;
&lt;br /&gt;
    Class Foo; end&lt;br /&gt;
    Class Bar, end&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the use of Abstract Factory Pattern&lt;br /&gt;
    &lt;br /&gt;
    def create_something(factory)&lt;br /&gt;
	new_object = factory.new&lt;br /&gt;
	puts &amp;quot;created a new #{new_object.class} with a factory&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
Here we select a factory to use&lt;br /&gt;
    Create_something(Foo)&lt;br /&gt;
    Create_something(Bar)&lt;br /&gt;
&lt;br /&gt;
Output of the code:&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Foo with a factory&amp;lt;br/&amp;gt;&lt;br /&gt;
Created a Bar with a factory&lt;br /&gt;
&lt;br /&gt;
The create_something method is creating objects through an abstract interface. It does not have details about implementation used to create these objects. Thus the use of create_something() is used to shield the rest of the code from that knowledge.&lt;br /&gt;
&lt;br /&gt;
===Implementation in Java===&lt;br /&gt;
In Java, implementing  Abstract Factory design pattern we need to create a class which has method who defers creation of product objects to its concrete class. This class then needs to be ”extended” by the client class which uses only these interfaces to create objects of concrete class&lt;br /&gt;
[http://userpages.umbc.edu/~tarr/dp/lectures/Factory-2pp.pdf]. In Java, Abstract Factory defines a different method for the creation of each product it can produce. The following example shows an implementation of Abstract Factory design pattern in Java. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class FactoryFmProto {&lt;br /&gt;
&lt;br /&gt;
static class Expression {&lt;br /&gt;
   protected String str;&lt;br /&gt;
   public Expression( String s ) { str = s; }&lt;br /&gt;
   public Expression cloan()     { return null; }&lt;br /&gt;
   public String     toString()  { return str; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static abstract class Factory {&lt;br /&gt;
   protected Expression prototype = null;&lt;br /&gt;
   public Expression makePhrase() { return prototype.cloan(); }&lt;br /&gt;
   public abstract Expression makeCompromise();&lt;br /&gt;
   public abstract Expression makeGrade();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static class PCFactory extends Factory {&lt;br /&gt;
   public PCFactory() { prototype = new PCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;do it your way, any way, or no way\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;you pass, self-esteem intact\&amp;quot;&amp;quot; ); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static class NotPCFactory extends Factory {&lt;br /&gt;
   public NotPCFactory() { prototype = new NotPCPhrase(); }&lt;br /&gt;
   public Expression makeCompromise() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;my way, or the highway\&amp;quot;&amp;quot; ); }&lt;br /&gt;
   public Expression makeGrade() {&lt;br /&gt;
      return new Expression( &amp;quot;\&amp;quot;take test, deal with the results\&amp;quot;&amp;quot; ); }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public static void main( String[] args ) {&lt;br /&gt;
   Factory factory;&lt;br /&gt;
   if (args.length &amp;gt; 0) factory = new PCFactory();&lt;br /&gt;
   else                 factory = new NotPCFactory();&lt;br /&gt;
   for (int i=0; i &amp;lt; 3; i++) System.out.print( factory.makePhrase() + &amp;quot;  &amp;quot; );&lt;br /&gt;
   System.out.println();&lt;br /&gt;
   System.out.println( factory.makeCompromise() );&lt;br /&gt;
   System.out.println( factory.makeGrade() );&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
===Comparison of Implementations===&lt;br /&gt;
Thus Java implementation needs a well defined interface to do so but in Ruby it is directly implemented because of private class object property.&lt;br /&gt;
&lt;br /&gt;
=Iterator Design Pattern=&lt;br /&gt;
An Iterator object encapsulates the internal structure of how the iteration occurs. It is a type of Behavioral design pattern. [http://en.wikipedia.org/wiki/Iterator_pattern]. “Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.” [http://sourcemaking.com/design_patterns/iterator]&lt;br /&gt;
&lt;br /&gt;
Ruby implements iterators with blocks and the ‘each’ method, and with ‘for..in’ statements. For example consider the following example;&lt;br /&gt;
&lt;br /&gt;
    def print_element(container)&lt;br /&gt;
    	Container.each {|o| puts o.inspect }&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    list = [1,2,2,3]&lt;br /&gt;
    hash = {“a”=&amp;gt;1, “b”=&amp;gt;2,”c”=&amp;gt;3, “d”=&amp;gt;4 }&lt;br /&gt;
    print_elements list&lt;br /&gt;
    print_elements hash&lt;br /&gt;
&lt;br /&gt;
The output of the code is,&lt;br /&gt;
    1&lt;br /&gt;
    2&lt;br /&gt;
    3&lt;br /&gt;
    4&lt;br /&gt;
    [“a”,1]&lt;br /&gt;
    [“b”,2]&lt;br /&gt;
    [“c”,3]&lt;br /&gt;
    [“d”,4]&lt;br /&gt;
&lt;br /&gt;
In Java implementing Iterator design pattern would again involve having an interface for accessing and traversing the elements which is further implemented by the concrete class.  Thus a class who needs to access the list will need to call the interface class. [http://userpages.umbc.edu/~tarr/dp/lectures/Iterator-2pp.pdf] Thus Ruby helps in implementing this design pattern easily by providing functions like “each” which does the handling of the concrete class implementation.&lt;br /&gt;
&lt;br /&gt;
=Decorator Pattern=&lt;br /&gt;
It is used to increase the functionality of the existing object dynamically. It adds the behavior at runtime.&lt;br /&gt;
The below code is an example of decorators where we just create a place holder and we can use this function to perform different functions depending on the parameters supplied to it. &lt;br /&gt;
The need for this kind of pattern is so as to increase the functionality of a particular class. &lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Ruby==&lt;br /&gt;
In this example we talk about how a coffee class can have many addtions to it like coffee with cream, sprinkles, milk etc and creating a class for each of them will not be the correct solution.In this example the cost can be calculated according to what is sent to it.Cost of the coffee is 2 and the cost of White coffee (coffee + milk) = 2.4. So what happens when we execute the Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost we get the result as 2.9 as the cost of the coffee is 2, the decorator object (now only coffee) is sent to the Milk and it becomes 2.4, which now is then sent to the Whip which is going to be 2.4 + 02 = 2.6 and finally to Sprinkles which becomes 2.6 + 0.3 = 2.9.&lt;br /&gt;
    module Decorator&lt;br /&gt;
      def initialize(decorated)&lt;br /&gt;
        @decorated = decorated&lt;br /&gt;
      end&lt;br /&gt;
&lt;br /&gt;
    def method_missing(method, *args)&lt;br /&gt;
        args.empty? ? @decorated.send(method) : @decorated.send(method, args)&lt;br /&gt;
    end&lt;br /&gt;
   end&lt;br /&gt;
&lt;br /&gt;
    class Whip&lt;br /&gt;
      include Decorator&lt;br /&gt;
      def cost &lt;br /&gt;
        @decorated.cost + 0.2&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    class Sprinkles&lt;br /&gt;
      include Decorator&lt;br /&gt;
&lt;br /&gt;
      def cost&lt;br /&gt;
        @decorated.cost + 0.3&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
&lt;br /&gt;
    Whip.new(Coffee.new).cost&lt;br /&gt;
    #=&amp;gt; 2.2&lt;br /&gt;
    Sprinkles.new(Whip.new(Milk.new(Coffee.new))).cost &lt;br /&gt;
    #=&amp;gt; 2.9&lt;br /&gt;
&lt;br /&gt;
==Decorator Example in Java==&lt;br /&gt;
The components for this example are described below&lt;br /&gt;
* Component: Defines the interface for objects that can have responsibilities added to them dynamically.&lt;br /&gt;
* ConcreteComponent: Defines an object to which additional responsibilities can be attached.&lt;br /&gt;
* Decorator: maintains a reference to a Component object and defines an interface that conforms to Component's interface.&lt;br /&gt;
* ConcreteDecorator: adds responsibilities to the component.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface IComponent {&lt;br /&gt;
    public void doStuff();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete component&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class Component implements IComponent{&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    System.out.println(&amp;quot;Do Suff&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Decorator&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public interface Decorator extends IComponent {&lt;br /&gt;
    public void addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
The Concrete Decorator ('''Extends IComponent''')&lt;br /&gt;
    package decorator;&lt;br /&gt;
    public class ConcreteDecorator implements Decorator {&lt;br /&gt;
    IComponent component;&lt;br /&gt;
    public ConcreteDecorator(IComponent component) {&lt;br /&gt;
    super();&lt;br /&gt;
    this.component = component;&lt;br /&gt;
    }&lt;br /&gt;
    public void addedBehavior() {&lt;br /&gt;
    System.out.println(&amp;quot;Decorator does some stuff too&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    public void doStuff() {&lt;br /&gt;
    component.doStuff();&lt;br /&gt;
    addedBehavior();&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
The Client&lt;br /&gt;
&lt;br /&gt;
    import decorator.*;&lt;br /&gt;
    public class Client {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
    IComponent comp = new Component();&lt;br /&gt;
    Decorator decorator = new ConcreteDecorator(comp);&lt;br /&gt;
    decorator.doStuff();&lt;br /&gt;
    }&lt;br /&gt;
    }&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22502</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22502"/>
		<updated>2009-10-07T05:20:03Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22500</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22500"/>
		<updated>2009-10-07T05:17:48Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source&amp;gt;&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22499</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22499"/>
		<updated>2009-10-07T05:17:12Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22498</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22498"/>
		<updated>2009-10-07T05:16:48Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&amp;lt;source lang=c&amp;gt;&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22497</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22497"/>
		<updated>2009-10-07T05:16:20Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&amp;lt;source lang=Ruby&amp;gt;&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22494</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22494"/>
		<updated>2009-10-07T05:09:55Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Factory */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22493</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22493"/>
		<updated>2009-10-07T05:08:57Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;br /&gt;
&lt;br /&gt;
=Factory=&lt;br /&gt;
Factories&lt;br /&gt;
The factory design pattern is an object oriented design pattern. It is a creational design pattern and deals with the issues faced in creating objects.&lt;br /&gt;
The main goal of this implementation is to isolate teh code that creates the class form the concete implementation of that class. Ruby example for the same is given below.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Example of Factory Code&lt;br /&gt;
{{{&lt;br /&gt;
class GearFactory&lt;br /&gt;
  def new() &lt;br /&gt;
    if ( ... some condition )&lt;br /&gt;
       return Sprocket.new()&lt;br /&gt;
    else&lt;br /&gt;
       return Cog().new()&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
Our client class now becomes:&lt;br /&gt;
class GearUser &lt;br /&gt;
  def doSomething(factory )&lt;br /&gt;
    ...&lt;br /&gt;
    my_gear = factory.new()&lt;br /&gt;
    ...&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
}}}&lt;br /&gt;
The above code does not have to distinguish between a factory and an ordinary class. We can call the class using the followijng code.&lt;br /&gt;
client.doSomething(GearFactory.new)          # Use the factory&lt;br /&gt;
client.doSomething(Cog)                      #Use the Cog class&lt;br /&gt;
client.doSomething(Sprocket)                 #Use the Sprocket class&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22492</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22492"/>
		<updated>2009-10-07T05:06:57Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a problem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22491</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22491"/>
		<updated>2009-10-07T05:05:34Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=Overview=&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a pronblem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22490</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 11 zv</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_11_zv&amp;diff=22490"/>
		<updated>2009-10-07T05:05:18Z</updated>

		<summary type="html">&lt;p&gt;Crazzyy: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;-Overview-&lt;br /&gt;
Before starting off with design patterns for Ruby we need to define what a design pattern is, Design patterns can be described as &amp;quot;a general reusable solution to a commonly occurring problem in software design.&amp;quot; [http://en.wikipedia.org/wiki/Design_pattern_%28computer_science%29]&lt;br /&gt;
The idea of design patterns is to not to reinvent the wheel but to solve the current problems by using solutions that have worked in the past.&lt;br /&gt;
A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design. It helps to identify the classes and instances and the way they collaborate with each other to form a solution to a pronblem.&lt;br /&gt;
Design patterns c an be classified into 3 parts&lt;br /&gt;
Creational, Structural, Behavioral (See if we can give links for these.)&lt;/div&gt;</summary>
		<author><name>Crazzyy</name></author>
	</entry>
</feed>