<?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>Hello Thupten &#187; &#187; design pattern</title>
	<atom:link href="https://www.thupten.feedback/tag/design-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.thupten.feedback</link>
	<description>Just another developer&#039;s blog</description>
	<lastBuildDate>Sat, 20 Jun 2026 21:18:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Adapter design pattern</title>
		<link>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/</link>
		<comments>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/#respond</comments>
		<pubDate>Sat, 15 Jan 2011 09:26:28 +0000</pubDate>
		<dc:creator><![CDATA[thupten]]></dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[adapter design pattern]]></category>
		<category><![CDATA[design pattern]]></category>

		<guid isPermaLink="false">http://thupten.veryusefulinfo.com/?p=165</guid>
		<description><![CDATA[Adapter pattern adapts a piece of code to work with another. In real world, consider a battery charger for your phone. If you went for vacation to a somewhere in Europe, your charger won&#8217;t fit into the wall outlet. You would need a piece that can wrap your that fits into the socket in Europe. [&#8230;]]]></description>
				<content:encoded><![CDATA[<a href="http://thupten.feedback/wp-content/uploads/2011/01/adapterexample.jpg"><img src="http://thupten.feedback/wp-content/uploads/2011/01/adapterexample-300x122.jpg" alt="" title="adapter pattern" width="300" height="122" class="alignright size-medium wp-image-174" /></a>
Adapter pattern adapts a piece of code to work with another. In real world, consider a battery charger for your phone. If you went for vacation to a somewhere in Europe, your charger won&#8217;t fit into the wall outlet. You would need a piece that can wrap your that fits into the socket in Europe.

Adapter pattern is usually helpful to create new code that is compatible with old legacy code. Using adapter pattern the old code do not need to be changed to work with new code.
<span id="more-165"></span>
In the world of the plugs, there is US plug
<pre class="java"><!--more-->public class USPlug {
    public USPlug(){
        System.out.println("I am a US plug");
    }

    public void plugIn(){
        System.out.println("I fit into a square socket");
    }
}</pre>
And then there is Europe Plug
<pre class="java">public class EuropePlug {
	public EuropePlug(){
		System.out.println("I am a Europe Plug");
	}
	public void plugIn(){
		System.out.println("I fit into circle socket");
	}
}</pre>
We need to put a US plug into a Europe socket. So we need an adapter called UStoEuropeAdapter. This adapter extends the feature of US plug to be compatible with european socket.
So here is the adapter
<pre class="java">public class UStoEuropeAdapter implements USPluggable {

	EuropePlug eplug;

	public UStoEuropeAdapter(EuropePlug ep){
		this.eplug = ep;
		System.out.println("I am the UStoEurope Adapter");
	}

	public void plugIn() {
		this.eplug.plugIn();
	}

}</pre>
The USPluggable interface mandates a class which implements this to make sure to define method to plug into us socket. ie plugIntoSquareSocket()
<pre class="java">public interface USPluggable {
	public void plugIn();
}</pre>
The caller program creates europe plug, wraps it into the adapter and plugs it into the square socket. But since the adapter overwrites the method plugIntoSquareSocket() by calling the plugIntoCircleSocket() instead, now this adapter plugs into circle socket.
<pre class="java">public class MainProgram {
	public static void main(String[] args) {
		EuropePlug ep = new EuropePlug();
		USPluggable myAdapter = new UStoEuropeAdapter(ep);
		myAdapter.plugIn();
	}
}</pre>]]></content:encoded>
			<wfw:commentRss>https://www.thupten.feedback/2011/01/15/adapter-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singleton Pattern</title>
		<link>https://www.thupten.feedback/2010/06/20/singleton-pattern/</link>
		<comments>https://www.thupten.feedback/2010/06/20/singleton-pattern/#comments</comments>
		<pubDate>Sun, 20 Jun 2010 22:21:49 +0000</pubDate>
		<dc:creator><![CDATA[thupten]]></dc:creator>
				<category><![CDATA[Opensource]]></category>
		<category><![CDATA[Others]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[single object pattern]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[singleton pattern]]></category>

		<guid isPermaLink="false">http://thupten.veryusefulinfo.com/?p=83</guid>
		<description><![CDATA[In Singleton Pattern, the class can only create a single instance. We want a class to have only a single instance for various reasons. Sometimes, we want use a global object to keep information about your program. This object should not have any copies. This information might be things like configuration of the program, or [&#8230;]]]></description>
				<content:encoded><![CDATA[<a href="http://thupten.feedback/wp-content/uploads/2010/06/singleton.png"><img class="alignright size-full wp-image-86" title="singleton" src="http://thupten.feedback/wp-content/uploads/2010/06/singleton.png" alt="" width="200" height="240" /></a>In Singleton Pattern, the class can only create a single instance. We want a class to have only a single instance for various reasons.
Sometimes, we want use a global object to keep information about your program. This object should not have any copies. This information might be things like configuration of the program, or a master object that manages pools of resources. So when you need a resource, you ask the master object to get it for you. Now if there were many copies of this master object, you would not know whom to ask for that resource. This single object should not be allowed to have copies. Singleton Pattern forces this rule so that programmer doesn&#8217;t have to remember about not creating copies. Singleton pattern will create an instance if it doesn&#8217;t exist and will not create any new instance if an instance already exist. It will just return a reference to that single instance.
<pre name="code" class="java">
class ProgramConfiguration{
    public ProgramConfiguraiton(){
        //default constructor code
    }
}</pre>
<span id="more-83"></span>
A new instance of a class is created by the constructor. Most of the time, we have a public constructor, which is called to create a new instance. Since we want to prohibit multiple instance, we have to restrict access to the constructor. This is done by making the constructor private.
<pre name="code" class="java">
class ProgramConfiguration{
    private ProgramConfiguration(){
        //default private constructor code
    }
}</pre>
then we create a static public method that will make sure that only one instance lives in the whole program.
<pre name="code" class="java">
class ProgramConfiguration{
    private static ProgramConfiguration _configObject;
    private ProgramConfiguration(){
        //default private constructor code
    }
    public getInstance(){
        /*
        if an instance exist return that instance otherwise
        call the constructor to create an instance and return it.
        */
        if(_configObject == null){
            _configObject = ProgramConfiguration();
        }
    return _configObject;
    }
}</pre>
So anytime you want to get that single object, you call the getInstance() method.
<pre name="code" class="java">
public static void main(String[] args){
    /*
    no access to default constructor. so if you did
    ProgramConfiguration pc = new ProgramConfiguration();
    you will get compilation error.
    */
    ProgramConfiguration pc = ProgramConfiguration.getInstance();
    ProgramConfiguration newpc = ProgramConfiguration.getInstance();
    /*
    in the above code pc and newpc both point to the same static object. when
    getinstance() is called for the second time, it finds that _configObject is not null
    anymore, so it doesn't call the constructor to create any new instance.
    */
}</pre>]]></content:encoded>
			<wfw:commentRss>https://www.thupten.feedback/2010/06/20/singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Factory method design pattern</title>
		<link>https://www.thupten.feedback/2010/06/09/factory-method-design-pattern/</link>
		<comments>https://www.thupten.feedback/2010/06/09/factory-method-design-pattern/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 08:45:01 +0000</pubDate>
		<dc:creator><![CDATA[thupten]]></dc:creator>
				<category><![CDATA[OpenOffice]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[factory method design pattern]]></category>

		<guid isPermaLink="false">http://thupten.veryusefulinfo.com/?p=62</guid>
		<description><![CDATA[OpenOffice.org development heavily uses the Factory method design pattern. Design patterns are conventional templates that describes how to solve common software problems. Since most developers are familiar with the patterns, they can recognize a pattern in others source code. That makes working in teams easier. There are many popular design patterns. One of them is [&#8230;]]]></description>
				<content:encoded><![CDATA[<a href="http://thupten.feedback/wp-content/uploads/2010/06/Factory_1.png"><img class="size-full wp-image-74 alignright" title="Factory_1" src="http://thupten.feedback/wp-content/uploads/2010/06/Factory_1.png" alt="" width="200" height="165" /></a>OpenOffice.org development heavily uses the Factory method design pattern.

Design patterns are conventional templates that describes how to solve common software problems. Since most developers are familiar with the patterns, they can recognize a pattern in others source code. That makes working in teams easier. There are many popular design patterns. One of them is Factory method pattern.

Factory method pattern is a type of creational pattern. Creational pattern pattern solves problems related to creating. Factory pattern solves two major problem generally faced by developers.
<span id="more-62"></span>
To reduce too many new operator usage
<ol>
	<li>When working on a large software, numerous instances of classes are created continuously at the runtime. The programmer cannot predict what the user is going to do. So at any given time, the programmer doesn&#8217;t know what object is create. For example, To create a new document, the user might click new text document or new spreadsheet document. There would several possibilities about what the user is going to do. So, a factory class is assigned to do all  these repetitive work of creating a new instance of what the user wants. By separating these repetitive object creations into a factory class, when new classes are added, only the factory class need to be updated.</li>
	<li>To create object without knowing its class name.
When using the concrete classes, the developer has to remember the class names. In factory pattern, choosing what type of object to be created is delegated to the factory class. Usually this is done by sending a parameter. Based on the parameter passed to the factory, the factory creates an instance of a certain type/class.</li>
</ol>
<a href="http://thupten.feedback/wp-content/uploads/2010/06/factorypattern.gif"><img class="alignnone size-medium wp-image-65" title="factorypattern" src="http://thupten.feedback/wp-content/uploads/2010/06/factorypattern-300x151.gif" alt="" width="300" height="151" /></a>

Here is the pseudo code.
<pre name="code" class="java">
public final class DocumentFactory {
   XDocument document;
   XDocument getDocument(String type){
      if(type.equals("text"){
         document = new TextDocument();
      }
      else if(type.equals("sheet"){
         document = new SpreadSheet();
      }
   return document;
   }
}</pre>
<pre name="code" class="java">
public interface XDocument{
   open();
}</pre>
<pre name="code" class="java">
public class TextDocument implements XDocument{
   //concrete class for Text documents
   open(){
      //method to open text document
      System.out.println("opening a text document...");
   }
}</pre>
<pre name="code" class="java">
public class SpreadSheet implements XDocument{
   //concrete class for spreadsheet documents
   open(){
      //method to open spreadsheet document
      System.out.println("opening a spreadsheet document...");
   }
}</pre>
<pre name="code" class="java">
class DocumentProgram{
   public static void main(String[] args){
   //this just created an instance of TextDocument without knowing its class name.
   XDocument doc = df.getDocument("text");
   doc.open();
   }
}</pre>]]></content:encoded>
			<wfw:commentRss>https://www.thupten.feedback/2010/06/09/factory-method-design-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reading &#8216;Head First Design Patterns&#8217;</title>
		<link>https://www.thupten.feedback/2010/06/03/reading-head-first-design-patterns/</link>
		<comments>https://www.thupten.feedback/2010/06/03/reading-head-first-design-patterns/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 02:50:27 +0000</pubDate>
		<dc:creator><![CDATA[thupten]]></dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[read]]></category>

		<guid isPermaLink="false">http://thupten.veryusefulinfo.com/?p=59</guid>
		<description><![CDATA[It is sad that CPA in Seneca doesn&#8217;t offer any course about Software Design Patterns. I just read through chapter two about &#8216;Subject Observers Pattern&#8217;. Subject Observers pattern is useful when a bunch of observer objects need to be notified about change in the subject object. Observers registers themselves to get notifications from the Subject. [&#8230;]]]></description>
				<content:encoded><![CDATA[<a href="http://thupten.feedback/wp-content/uploads/2010/06/pattern.png"><img class="alignright size-full wp-image-79" title="pattern" src="http://thupten.feedback/wp-content/uploads/2010/06/pattern.png" alt="" width="200" height="231" /></a>It is sad that CPA in Seneca doesn&#8217;t offer any course about Software Design Patterns.

I just read through chapter two about &#8216;Subject Observers Pattern&#8217;.

Subject Observers pattern is useful when a bunch of observer objects need to be notified about change in the subject object. Observers registers themselves to get notifications from the Subject. When there is a change in any data in Subject, the subject notifies all the observers about the change. The subject object maintains a list of all the observer objects. When an observer unregisters itself, it is removed from this list. So next time, the subject notifies all the observers in its list, the unregisters are not sent any more notifications.

For example, a weather tracking system that sends temperature information to all the clients every time the temperature changes.]]></content:encoded>
			<wfw:commentRss>https://www.thupten.feedback/2010/06/03/reading-head-first-design-patterns/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
