<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Blog &#187; Design Patterns</title>
	<atom:link href="http://javapapers.com/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://javapapers.com</link>
	<description>BLOG on core java, servlets, JSP, design patterns interview questions with quality answers, source code and discussions.</description>
	<lastBuildDate>Tue, 27 Jul 2010 18:48:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Prototype Pattern</title>
		<link>http://javapapers.com/design-patterns/prototype-pattern/</link>
		<comments>http://javapapers.com/design-patterns/prototype-pattern/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 12:04:27 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=371</guid>
		<description><![CDATA[When creating an object is time consuming and a costly affair and you already have a most similar object instance in hand, then you go for prototype pattern. Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs.
Its a [...]]]></description>
			<content:encoded><![CDATA[<p>When creating an object is time consuming and a costly affair and you already have a most similar object instance in hand, then you go for prototype pattern. Instead of going through a time consuming process to create a complex object, just copy the existing similar object and modify it according to your needs.</p>
<p>Its a simple and straight forward design pattern. Nothing much hidden beneath it. If you don&#8217;t have much experience with enterprise grade huge application, you may not have experience in creating a complex / time consuming instance. All you might have done is use the new operator or inject and instantiate.</p>
<p>If you are a beginner you might be wondering, why all the fuss about prototye design pattern and do we really need this design pattern? Just ignore, all the big guys requires it. For you, just understand the pattern and sleep over it. You may require it one day in future.<img class="alignright size-full wp-image-372" title="prototype" src="http://javapapers.com/wp-content/uploads/2010/07/prototype.gif" alt="" width="294" height="294" /></p>
<p>Prototype pattern may look similar to<a title="Builder Design Pattern" href="http://javapapers.com/design-patterns/builder-pattern/"> builder design pattern</a>. There is a huge difference to it. If you remember, &#8220;the same construction process can create different representations&#8221; is the key in builder pattern. But not in the case of prototype pattern.</p>
<p>So, how to implement the prototype design pattern? You just have to copy the existing instance in hand. When you say copy in java, immediately cloning comes into picture. Thats why when you read about prototype pattern, all the literature invariably refers java cloning.</p>
<p>Simple way is, clone the existing instance in hand and then make the required update to the cloned instance so that you will get the object you need. Other way is, tweak the cloning method itself to suit your new object creation need. Therefore whenever you clone that object you will directly get the new object of desire without modifying the created object explicitly.</p>
<p>The prototype design pattern mandates that the instance which you are going to copy should provide the copying feature. It should not be done by an external utility or provider.</p>
<p>But the above, other way comes with a caution. If somebody who is not aware of your tweaking the clone business logic uses it, he will be in issue. Since what he has in hand is not the exact clone. You can go for a custom method which calls the clone internally and then modifies it according to the need. Which will be a better approach.</p>
<p>Always remember while using clone to copy, whether you need a <a title="Java Clone, Shallow Copy and Deep Copy" href="http://javapapers.com/core-java/java-clone-shallow-copy-and-deep-copy/">shallow copy or deep copy</a>. Decide based on your business needs. If you need a deep copy, you can use serialization as a hack to get the deep copy done. Using clone to copy is entirey a design decision while implementing the prototype design pattern. Clone is not a mandatory choice for prototype pattern.</p>
<p>In prototype pattern, you should always make sure that you are well knowledgeable about the data of the object that is to be cloned. Also make sure that instance allows you to make changes to the data. If not, after cloning you will not be able to make required changes to get the new required object.</p>
<p>Following sample java source code demonstrates the prototype pattern. I have a basic bike in hand with four gears. When I want to make a different object, an advance bike with six gears I copy the existing instance. Then make necessary modifications to the copied instance. Thus the prototype pattern is implemented. Example source code is just to demonstrate the design pattern, please don&#8217;t read too much out of it. I wanted to make things as simple as possible.</p>
<h2>Sample Java Source Code for Prototype Design Pattern</h2>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.prototype;

class Bike implements Cloneable {
	private int gears;
	private String bikeType;
	private String model;
	public Bike() {
		bikeType = &quot;Standard&quot;;
		model = &quot;Leopard&quot;;
		gears = 4;
	}

	public Bike clone() {
		return new Bike();
	}

	public void makeAdvanced() {
		bikeType = &quot;Advanced&quot;;
		model = &quot;Jaguar&quot;;
		gears = 6;
	}
	public String getModel(){
		return model;
	}
}

public class Workshop {
	public Bike makeJaguar(Bike basicBike) {
		basicBike.makeAdvanced();
		return basicBike;
	}
	public static void main(String args[]){
		Bike bike = new Bike();
		Bike basicBike = bike.clone();
		Workshop workShop = new Workshop();
		Bike advancedBike = workShop.makeJaguar(basicBike);
		System.out.println(&quot;Prototype Design Pattern: &quot;+advancedBike.getModel());
	}
}
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Prototype+Pattern+-+http://javapapers.com/design-patterns/prototype-pattern/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://javapapers.com/design-patterns/prototype-pattern/&amp;t=Prototype+Pattern" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://javapapers.com/design-patterns/prototype-pattern/&amp;title=Prototype+Pattern" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://javapapers.com/design-patterns/prototype-pattern/&amp;title=Prototype+Pattern" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://javapapers.com/design-patterns/prototype-pattern/&amp;title=Prototype+Pattern" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://javapapers.com/design-patterns/prototype-pattern/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Prototype%20Pattern%22&amp;body=Link: http://javapapers.com/design-patterns/prototype-pattern/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A When%20creating%20an%20object%20is%20time%20consuming%20and%20a%20costly%20affair%20and%20you%20already%20have%20a%20most%20similar%20object%20instance%20in%20hand%2C%20then%20you%20go%20for%20prototype%20pattern.%20Instead%20of%20going%20through%20a%20time%20consuming%20process%20to%20create%20a%20complex%20object%2C%20just%20copy%20the%20existing%20similar%20object%20and%20modify%20it%20according%20to" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/design-patterns/prototype-pattern/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Builder Pattern</title>
		<link>http://javapapers.com/design-patterns/builder-pattern/</link>
		<comments>http://javapapers.com/design-patterns/builder-pattern/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 15:51:04 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=277</guid>
		<description><![CDATA[Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.
For example, you can consider construction of a home. Home is the final [...]]]></description>
			<content:encoded><![CDATA[<p>Builder pattern is used to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create different representations of the same object.</p>
<div id="attachment_278" class="wp-caption aligncenter" style="width: 260px"><a href="http://javapapers.com/wp-content/uploads/2009/11/ComplexObject.jpg"><img class="size-full wp-image-278" title="ComplexObject" src="http://javapapers.com/wp-content/uploads/2009/11/ComplexObject.jpg" alt="Complex Object Construction" width="250" height="219" /></a><p class="wp-caption-text">Complex Object Construction</p></div>
<p>For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally the whole home object is returned. Here using the same process you can build houses with different properties.</p>
<p>GOF says,</p>
<blockquote><p>&#8220;Separate the construction of a complex object from its representation so that the same construction process can create different representations&#8221; [GoF 94]</p></blockquote>
<h3>What is the difference between abstract factory and builder pattern?</h3>
<p><a title="Abstract Factory Pattern" href="http://javapapers.com/design-patterns/abstract-factory-pattern/">Abstract factory</a> may also be used to construct a complex object, then what is the difference with builder pattern? In builder pattern emphasis is on &#8217;step by step&#8217;. Builder pattern will have many number of small steps. Those every steps will have small units of logic enclosed in it. There will also be a sequence involved. It will start from step 1 and will go on upto step n and the final step is returning the object. In these steps, every step will add some value in construction of the object. That is you can imagine that the object grows stage by stage. Builder will return the object in last step. But in abstract factory how complex the built object might be, it will not have step by step object construction.</p>
<h2>Sample builder design pattern implementation in Java API</h2>
<p><a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilderFactory.html">DocumentBuilderFactory</a> , StringBuffer, StringBuilder are some examples of builder pattern usage in java API.</p>
<h2>Sample Java Source Code for Builder Pattern</h2>
<p>Following is the interface, that will be returned as the product from the builder.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public interface HousePlan {

	public void setBasement(String basement);

	public void setStructure(String structure);

	public void setRoof(String roof);

	public void setInterior(String interior);
}
</pre>
<p>Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public interface AnimalFactory {
	public Animal createAnimal();
}
</pre>
<p>Concrete class for the above interface. The builder constructs an implementation for the following class.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public class House implements HousePlan {

	private String basement;
	private String structure;
	private String roof;
	private String interior;

	public void setBasement(String basement) {
		this.basement = basement;
	}

	public void setStructure(String structure) {
		this.structure = structure;
	}

	public void setRoof(String roof) {
		this.roof = roof;
	}

	public void setInterior(String interior) {
		this.interior = interior;

	}

}
</pre>
<p>Builder interface. We will have multiple different implementation of this interface in order to facilitate, the same construction process to create different representations.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public interface HouseBuilder {

	public void buildBasement();

	public void buildStructure();

	public void bulidRoof();

	public void buildInterior();

	public House getHouse();
}
</pre>
<p>First implementation of a builder.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public class IglooHouseBuilder implements HouseBuilder {

	private House house;

	public IglooHouseBuilder() {
		this.house = new House();
	}

	public void buildBasement() {
		house.setBasement(&quot;Ice Bars&quot;);
	}

	public void buildStructure() {
		house.setStructure(&quot;Ice Blocks&quot;);
	}

	public void buildInterior() {
		house.setInterior(&quot;Ice Carvings&quot;);
	}

	public void bulidRoof() {
		house.setRoof(&quot;Ice Dome&quot;);
	}

	public House getHouse() {
		return this.house;
	}
}
</pre>
<p>Second implementation of a builder. Tipi is a type of eskimo house.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public class TipiHouseBuilder implements HouseBuilder {
	private House house;

	public TipiHouseBuilder() {
		this.house = new House();
	}

	public void buildBasement() {
		house.setBasement(&quot;Wooden Poles&quot;);
	}

	public void buildStructure() {
		house.setStructure(&quot;Wood and Ice&quot;);
	}

	public void buildInterior() {
		house.setInterior(&quot;Fire Wood&quot;);
	}

	public void bulidRoof() {
		house.setRoof(&quot;Wood, caribou and seal skins&quot;);
	}

	public House getHouse() {
		return this.house;
	}

}
</pre>
<p>Following class constructs the house and most importantly, this maintains the building sequence of object.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public class CivilEngineer {

	private HouseBuilder houseBuilder;

	public CivilEngineer(HouseBuilder houseBuilder){
		this.houseBuilder = houseBuilder;
	}

	public House getHouse() {
		return this.houseBuilder.getHouse();
	}

	public void constructHouse() {
		this.houseBuilder.buildBasement();
		this.houseBuilder.buildStructure();
		this.houseBuilder.bulidRoof();
		this.houseBuilder.buildInterior();
	}
}
</pre>
<p>Testing the sample builder design pattern.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.builder;

public class BuilderSample {
	public static void main(String[] args) {
		HouseBuilder iglooBuilder = new IglooHouseBuilder();
		CivilEngineer engineer = new CivilEngineer(iglooBuilder);

		engineer.constructHouse();

		House house = engineer.getHouse();

		System.out.println(&quot;Builder constructed: &quot;+house);
	}
}
</pre>
<h3>Output of the above sample program for builder pattern</h3>
<pre class="brush: plain;">Builder constructed: com.javapapers.sample.designpattern.builder.House@7d772e
</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Builder+Pattern+-+http://javapapers.com/design-patterns/builder-pattern/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://javapapers.com/design-patterns/builder-pattern/&amp;t=Builder+Pattern" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://javapapers.com/design-patterns/builder-pattern/&amp;title=Builder+Pattern" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://javapapers.com/design-patterns/builder-pattern/&amp;title=Builder+Pattern" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://javapapers.com/design-patterns/builder-pattern/&amp;title=Builder+Pattern" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://javapapers.com/design-patterns/builder-pattern/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Builder%20Pattern%22&amp;body=Link: http://javapapers.com/design-patterns/builder-pattern/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Builder%20pattern%20is%20used%20to%20construct%20a%20complex%20object%20step%20by%20step%20and%20the%20final%20step%20will%20return%20the%20object.%20The%20process%20of%20constructing%20an%20object%20should%20be%20generic%20so%20that%20it%20can%20be%20used%20to%20create%20different%20representations%20of%20the%20same%20object.%0D%0A%0D%0A%0D%0A%0D%0AFor%20example%2C%20you%20can%20consider%20construction%20of%20a%20" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/design-patterns/builder-pattern/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Abstract Factory Pattern</title>
		<link>http://javapapers.com/design-patterns/abstract-factory-pattern/</link>
		<comments>http://javapapers.com/design-patterns/abstract-factory-pattern/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 15:58:54 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=254</guid>
		<description><![CDATA[Factory of factories. To keep things simple you can understand it like, you have a set of &#8216;related&#8217; factory method design pattern. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can [...]]]></description>
			<content:encoded><![CDATA[<p>Factory of factories. To keep things simple you can understand it like, you have a set of &#8216;related&#8217; <a title="Factory Method Design Pattern" href="http://javapapers.com/design-patterns/factory-method-pattern/">factory method design pattern</a>. Then you will put all those set of simple factories inside a factory pattern. So in turn you need not be aware of the final concrete class that will be instantiated. You can program for the interface using the top factory.</p>
<p>There is also a view that abstract factory is &#8216;also&#8217; implemented using prototype instead of factory methords pattern. Beginners for now please don&#8217;t yourself with that. Just go with factory methods pattern.</p>
<p>As there is a word &#8216;abstract&#8217; in the pattern name don&#8217;t mistake and confuse it with java &#8216;abstract&#8217; keyword. It is not related to that. This abstract is from object oriented programming paradim.</p>
<h2>Sample abstract factory design pattern implementation in Java API</h2>
<p>XML API implements abstract factory. There is a class name <a title="Abstract Factory Implementation in Java API" href="http://java.sun.com/javase/6/docs/api/javax/xml/validation/SchemaFactory.html">SchemaFactory</a>. This acts as a factory and supports implemenation of multiple schemas using abstract factory design pattern.</p>
<h2>Sample Java Source Code for Factory Method Design Pattern</h2>
<p>Following is the interface, that will be returned as the final end product from the factories.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public interface Animal {
	public void breathe();
}
</pre>
<p>Following is the interface for which the factory implementation should be done. Inturn all abstract factory will return this type.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public interface AnimalFactory {
	public Animal createAnimal();
}
</pre>
<p>One of the factory from a predefined set which will instantiate the above interface.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class SeaFactory implements AnimalFactory {

	public Animal createAnimal() {
		return new Shark();
	}

}
</pre>
<p>Second factory from a predefined set which will instantiate the Animal interface.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class LandFactory implements AnimalFactory {
	public Animal createAnimal() {
		return new Elephant();
	}
}
</pre>
<p>Implementation of an Animal. This class is grouped with the first abstract factory.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class Shark implements Animal {
	public void breathe() {
		System.out.println(&quot;I breathe in water! He he!&quot;);
	}
}
</pre>
<p>Implementation of an Animal. This class is grouped with the second abstract factory.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class Elephant implements Animal {
	public void breathe() {
		System.out.println(&quot;I breathe with my lungs. Its easy!&quot;);
	}
}
</pre>
<p>Following class consumes the abstract factory.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class Wonderland {
	public Wonderland(AnimalFactory factory) {
		Animal animal = factory.createAnimal();
		animal.breathe();
	}
}
</pre>
<p>Testing the abstract factory design pattern.</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.abstractfactory;

public class SampleAbstractFactory {

	public static void main(String args[]){
		new Wonderland(createAnimalFactory(&quot;water&quot;));
	}

	public static AnimalFactory createAnimalFactory(String type){
		if(&quot;water&quot;.equals(type))
			return new SeaFactory();
		else
			return new LandFactory();
	}
}
</pre>
<h3>Output of the above sample program for abstract factory pattern</h3>
<pre class="brush: plain;">I breathe in water! He he!</pre>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Abstract+Factory+Pattern+-+http://javapapers.com/design-patterns/abstract-factory-pattern/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://javapapers.com/design-patterns/abstract-factory-pattern/&amp;t=Abstract+Factory+Pattern" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://javapapers.com/design-patterns/abstract-factory-pattern/&amp;title=Abstract+Factory+Pattern" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://javapapers.com/design-patterns/abstract-factory-pattern/&amp;title=Abstract+Factory+Pattern" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://javapapers.com/design-patterns/abstract-factory-pattern/&amp;title=Abstract+Factory+Pattern" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://javapapers.com/design-patterns/abstract-factory-pattern/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Abstract%20Factory%20Pattern%22&amp;body=Link: http://javapapers.com/design-patterns/abstract-factory-pattern/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Factory%20of%20factories.%20To%20keep%20things%20simple%20you%20can%20understand%20it%20like%2C%20you%20have%20a%20set%20of%20%27related%27%20factory%20method%20design%20pattern.%20Then%20you%20will%20put%20all%20those%20set%20of%20simple%20factories%20inside%20a%20factory%20pattern.%20So%20in%20turn%20you%20need%20not%20be%20aware%20of%20the%20final%20concrete%20class%20that%20will%20be%20instantiated.%20You" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/design-patterns/abstract-factory-pattern/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Factory Method Pattern</title>
		<link>http://javapapers.com/design-patterns/factory-method-pattern/</link>
		<comments>http://javapapers.com/design-patterns/factory-method-pattern/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 14:46:29 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=231</guid>
		<description><![CDATA[A factory method pattern is a creational pattern. It is used to instantiate an object from one among a set of classes based on a logic.
Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one or [...]]]></description>
			<content:encoded><![CDATA[<p>A factory method pattern is a creational pattern. It is used to instantiate an object from one among a set of classes based on a logic.</p>
<p>Assume that you have a set of classes which extends a common super class or interface. Now you will create a concrete class with a method which accepts one or more arguments. This method is our factory method. What it does is, based on the arguments passed factory method does logical operations and decides on which sub class to instantiate. This factory method will have the super class as its return type. So that, you can program for the interface and not for the implementation. This is all about factory method design pattern.</p>
<h2>Sample factory method design pattern implementation in Java API</h2>
<p>For a reference of how the factory method design pattern is implemented in Java, you can have a look at SAXParserFactory. It is a factory class which can be used to intantiate SAX based parsers to pares XML.  The method <a title="Java API - Factory Sample" href="http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/SAXParserFactory.html#newInstance%28%29">newInstance</a> is the factory method which instantiates the sax parsers based on some predefined logic.</p>
<h3>Block diagram for The Design Pattern</h3>
<p><img class="aligncenter size-full wp-image-232" title="factorydesignpattern" src="http://javapapers.com/wp-content/uploads/2009/11/factorydesignpattern.jpg" alt="factorydesignpattern" width="350" height="219" /></p>
<h2>Sample Java Source Code for Factory Method Design Pattern</h2>
<p>Based on comments received from users, I try to keep my sample java source code as simple as possible for a novice to understand.</p>
<p>Base class:</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.factorymethod;

//super class that serves as type to be instantiated for factory method pattern
public interface Pet {

 public String speak();

}
</pre>
<p>First subclass:</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.factorymethod;

//sub class 1 that might get instantiated by a factory method pattern
public class Dog implements Pet {

 public String speak() {
 return &quot;Bark bark...&quot;;
 }
}
</pre>
<p>Second subclass:</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.factorymethod;

//sub class 2 that might get instantiated by a factory method pattern
public class Duck implements Pet {
 public String speak() {
 return &quot;Quack quack...&quot;;
 }
}
</pre>
<p>Factory class:</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.factorymethod;

//Factory method pattern implementation that instantiates objects based on logic
public class PetFactory {

 public Pet getPet(String petType) {
 Pet pet = null;

 // based on logic factory instantiates an object
 if (&quot;bark&quot;.equals(petType))
 pet = new Dog();
 else if (&quot;quack&quot;.equals(petType))
 pet = new Duck();
 return pet;
 }
}
</pre>
<p>Using the factory method to instantiate</p>
<pre class="brush: java;">
package com.javapapers.sample.designpattern.factorymethod;

//using the factory method pattern
public class SampleFactoryMethod {

 public static void main(String args[]){

 //creating the factory
 PetFactory petFactory = new PetFactory();

 //factory instantiates an object
 Pet pet = petFactory.getPet(&quot;bark&quot;);

 //you don't know which object factory created
 System.out.println(pet.speak());
 }

}
</pre>
<h3>Output of the above sample program for Factory Method Pattern</h3>
<pre class="brush: plain;">
Bark bark
</pre>
<h2><a class="downloadlink" title="Sample Java Source Code For Factory Method Design Pattern" href="http://javapapers.com/wp-content/uploads/2009/11/factorydesignpattern.zip">Download Java Source Code For Factory Method Pattern</a></h2>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Factory+Method+Pattern+-+http://javapapers.com/design-patterns/factory-method-pattern/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://javapapers.com/design-patterns/factory-method-pattern/&amp;t=Factory+Method+Pattern" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://javapapers.com/design-patterns/factory-method-pattern/&amp;title=Factory+Method+Pattern" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://javapapers.com/design-patterns/factory-method-pattern/&amp;title=Factory+Method+Pattern" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://javapapers.com/design-patterns/factory-method-pattern/&amp;title=Factory+Method+Pattern" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://javapapers.com/design-patterns/factory-method-pattern/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Factory%20Method%20Pattern%22&amp;body=Link: http://javapapers.com/design-patterns/factory-method-pattern/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A A%20factory%20method%20pattern%20is%20a%20creational%20pattern.%20It%20is%20used%20to%20instantiate%20an%20object%20from%20one%20among%20a%20set%20of%20classes%20based%20on%20a%20logic.%0D%0A%0D%0AAssume%20that%20you%20have%20a%20set%20of%20classes%20which%20extends%20a%20common%20super%20class%20or%20interface.%20Now%20you%20will%20create%20a%20concrete%20class%20with%20a%20method%20which%20accepts%20one%20or%20mo" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/design-patterns/factory-method-pattern/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Introduction To Design Patterns</title>
		<link>http://javapapers.com/design-patterns/introduction-to-design-patterns/</link>
		<comments>http://javapapers.com/design-patterns/introduction-to-design-patterns/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 16:41:20 +0000</pubDate>
		<dc:creator>Joe</dc:creator>
				<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://javapapers.com/?p=227</guid>
		<description><![CDATA[Pattern is a defined, used and tested solution for a know problem. Design patterns is all about re-use. Software design patterns evolved as a subject of study only when object oriented programming started becoming popular. OOPS and design patterns became inseparable.
In OOPS, we should have well defined boundaries for objects. That is every object should [...]]]></description>
			<content:encoded><![CDATA[<p>Pattern is a defined, used and tested solution for a know problem. Design patterns is all about re-use. Software design patterns evolved as a subject of study only when object oriented programming started becoming popular. OOPS and design patterns became inseparable.</p>
<p>In OOPS, we should have well defined boundaries for objects. That is every object should have its roles and responsibilities well defined. Then at next level, we should have a clear interaction plan between objects. If you design a OO software with the above principle, then by default you will be following some of the already defined design patterns.</p>
<p>A formal definition for design patterns, &#8220;A design pattern addresses a recurring design problem that arises in specific design situations and presents a solution to it&#8221; (Buschmann, et. al. 1996)</p>
<p>Java widely uses design patterns in its APIs. It started as early as Java 1.2 in java foundation classes. By then you can see the widespread use of commonly know design patterns in collections framework and IO packages. When I say commonly known design patterns, I mention about the set of 23 design patterns by Gang of Four (GOF). Gamma, Helm, Johnson and Vlissides known as Gang of Four (GOF) published a book &#8220;Design Patterns &#8212; Elements of Reusable Software&#8221; (1995) based on their series of technical meetings. It is one of the best seller in computer science books till date.</p>
<p>In China gang of four means different set of people. Jiang Qing (Mao Zedong&#8217;s fourth wife), Zhang Chunqiao, Yao Wenyuan, and Wang Hongwen were very popular leaders of cultural revolution. They almost seized power after Mao Zedong&#8217;s death. But they were finally arrested and imprisoned for life.</p>
<p>Our GOF divided the 23 design patterns into three types creational design patterns, structural design patterns and behavioral design patterns.</p>
<p>Creational design patterns can be used to instantiate objects. Instead of instantiating objects directly, depending on scenario either X or Y object can be instantiated. This will give flexibility for instantiation in high complex business logic situations.</p>
<p>Structural design patterns can be used to organize your program into groups. This segregation will provide you clarity and will enable you for easier maintainability.</p>
<p>Behavioral design patterns can be used to define the communication and control flow between objects.</p>
<p>Following this post, I have planned to write a series of article on these design patterns with java source code examples and UML diagrams. Looking forward to your comments.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Introduction+To+Design+Patterns+-+http://javapapers.com/design-patterns/introduction-to-design-patterns/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://javapapers.com/design-patterns/introduction-to-design-patterns/&amp;t=Introduction+To+Design+Patterns" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://javapapers.com/design-patterns/introduction-to-design-patterns/&amp;title=Introduction+To+Design+Patterns" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://javapapers.com/design-patterns/introduction-to-design-patterns/&amp;title=Introduction+To+Design+Patterns" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://javapapers.com/design-patterns/introduction-to-design-patterns/&amp;title=Introduction+To+Design+Patterns" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-technorati">
			<a href="http://technorati.com/faves?add=http://javapapers.com/design-patterns/introduction-to-design-patterns/" rel="nofollow" class="external" title="Share this on Technorati">Share this on Technorati</a>
		</li>
		<li class="shr-mail">
			<a href="mailto:?subject=%22Introduction%20To%20Design%20Patterns%22&amp;body=Link: http://javapapers.com/design-patterns/introduction-to-design-patterns/ (sent via shareaholic)%0D%0A%0D%0A----%0D%0A Pattern%20is%20a%20defined%2C%20used%20and%20tested%20solution%20for%20a%20know%20problem.%20Design%20patterns%20is%20all%20about%20re-use.%20Software%20design%20patterns%20evolved%20as%20a%20subject%20of%20study%20only%20when%20object%20oriented%20programming%20started%20becoming%20popular.%20OOPS%20and%20design%20patterns%20became%20inseparable.%0D%0A%0D%0AIn%20OOPS%2C%20we%20should%20have%20well%20" rel="nofollow" class="external" title="Email this to a friend?">Email this to a friend?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://javapapers.com/design-patterns/introduction-to-design-patterns/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
