<?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>FactoryPattern.com &#187; Design Patterns</title>
	<atom:link href="http://www.factorypattern.com/category/design-patterns/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.factorypattern.com</link>
	<description>Just another Object Oriented Weblog</description>
	<lastBuildDate>Sat, 02 Jan 2010 22:08:06 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Method Chaining</title>
		<link>http://www.factorypattern.com/method-chaining/</link>
		<comments>http://www.factorypattern.com/method-chaining/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 00:48:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/?p=64</guid>
		<description><![CDATA[Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different [...]]]></description>
			<content:encoded><![CDATA[<p>Method chaining is a simple programming technique that can be implemented in almost any programming language. In simple words, it means that a method performs some operations on &#8220;this&#8221; object and then returns it so it can be used further more. It allows us to invoke several methods one after another, on one or different objects, usually written in the same line.</p>
<p>For example we can build a rectangle class using using the Method Chaining for setters. You can see the setters are a little bit different than regular setters:</p>
<pre name="code" class="java">class ChainedRectangle
{
    protected int x;
    public ChainedRectangle setX(int x) { this.x = x; return this; }

    protected int y;
    public ChainedRectangle setY(int y) { this.y = y; return this; }

    protected int width;
    public ChainedRectangle setWidth(int width) { this.width = width; return this; }

    protected int height;
    public ChainedRectangle setHeight(int height) { this.height = height; return this; }

    protected String color;
    public ChainedRectangle setColor(String color) { this.color = color; return this; }
}
</pre>
<p>Then if we need to build a rectangle we can write a single line for setting all the required values:</p>
<pre name="code" class="java">new         Rectangle().setX(10).setY(10).setWidth(13).setHeight(15).setColor("red");</pre>
<p>Along with autocomplete option in most of todays IDEs method chaining might provide a suggestive way of doing some actions in less code.<br />
<span id="more-64"></span><br />
For example we can write a simple class to build  sql queries. Then we can invoke in a simple way.</p>
<pre name="code" class="java">public class Query
{
    protected String queryString = "";

    public Query select(String what)
    {
        queryString = "SELECT " + what;
    }

    public Query from(String from)
    {
        queryString = " FROM " + from;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>Then, to build the query we invoke:</p>
<pre name="code" class="java">new Query().select("*").from("table").where("a=b").Invoke();</pre>
<p>One of the problems of the Method Chaining pattern is the fact that it can not ensure the order of the invoked method. In order to do that each method can return a specific interface object which implements only the allowed methods.</p>
<p>For example we define an interface ISelectedQuery with a single method from, IFromQuery with a single method where. ISelectedQuery.from returns and object of type IFromQuery and IFromQuery returns a Query object. All the interfaces are implemented by the Query object. That way the order of invoking the methods is ensured.</p>
<p>Here is how the code looks like:</p>
<pre name="code" class="java">public interface ISelectedQuery
{
    IFromQuery from(String from);
}

public interface IFromQuery
{
    Query where(String where);
}

 public class Query implements ISelectedQuery, IFromQuery
{
    protected String queryString = "";

    public ISelectedQuery select(String what)
    {
        queryString = "SELECT " + what;
        return this;
    }

    public IFromQuery from(String from)
    {
        queryString = " FROM " + from;
        return this;
    }

    public Query where(String where)
    {
        queryString = " WHERE " + where;
        return this;
    }

    public ResultSet Invoke()
    {
        //... database invocation bla bla
        return result;
    }
}</pre>
<p>The above code ensure that only the right methods can be invoked. Further more the syntax autocomplete option from most of today IDEs will show in the drop down only the right method(s).</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/method-chaining/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/method-chaining/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li>No Related Post</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/method-chaining/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Using Visitor Patterns on non Visitable Structures</title>
		<link>http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/</link>
		<comments>http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/#comments</comments>
		<pubDate>Fri, 30 May 2008 07:27:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Visitor Pattern]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/</guid>
		<description><![CDATA[The visitor pattern can be used on structures of objects which implements a specific interface defining a method called accept. In practice, in many cases, the structures are already created and we have to visit structures of already created objects. Changing hierarchies of classes for adding a new method is not a viable solution.
We need [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.oodesign.com/visitor-pattern.html">visitor pattern</a> can be used on structures of objects which implements a specific interface defining a method called accept. In practice, in many cases, the structures are already created and we have to visit structures of already created objects. Changing hierarchies of classes for adding a new method is not a viable solution.</p>
<p>We need somehow to extend the structure of objects for accepting the visitors without changing them. A way of doing it would be to add a wrapper for the hierarchy classes which accepts visitors and duplicates the structure.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/" title="Drawbacks of Marker Interface Design Pattern.">Drawbacks of Marker Interface Design Pattern.</a></li><li><a href="http://www.factorypattern.com/factory-pattern/" title="FactoryPattern.com &#8211; on air">FactoryPattern.com &#8211; on air</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Simulate Multiple Inheritance in Java</title>
		<link>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/</link>
		<comments>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 09:45:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Inheritance]]></category>
		<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/</guid>
		<description><![CDATA[Java or .NET languages does not allow multiple inheritance and usually we don&#8217;t need multiple inheritance in our projects. However there are cases when inheriting from multiple classes is a necessity. There are a few tricks we can apply in order to be able to get what multiple inheritance gives us in the languages where [...]]]></description>
			<content:encoded><![CDATA[<p>Java or .NET languages does not allow multiple inheritance and usually we don&#8217;t need multiple inheritance in our projects. However there are cases when inheriting from multiple classes is a necessity. There are a few tricks we can apply in order to be able to get what multiple inheritance gives us in the languages where it is supported.<span id="more-12"></span></p>
<p>The great idea to achieve it is to use delegation instead of inheritance. Let&#8217;s consider we already have to classes we want to extend. We can use the inheritance so our new class will extend the first class. For the second class we can not use inheritance again, because we already inherit the first one, on the other side we want to overwrite some method implementations. The trick is to create an inner class extending the second class, and to delegate the calls from the main outer class to the inner class.</p>
<p>The technique can be &#8220;scaled&#8221; for  any number of classes. For each new class which we need to inherit, we need to create another inner class and delegate the request to it. The big drawback here is the amount of code that should be written for creating the inherited inner classes and the code for delegating all the requests which which in case of multiple inheritance are done by default.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/" title="Drawbacks of Marker Interface Design Pattern.">Drawbacks of Marker Interface Design Pattern.</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li><li><a href="http://www.factorypattern.com/multimap-in-google-collections-library/" title="Multimap in Google Collections Library">Multimap in Google Collections Library</a></li><li><a href="http://www.factorypattern.com/how-to-use-ant/" title="How To Use Ant">How To Use Ant</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawbacks of Marker Interface Design Pattern.</title>
		<link>http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/</link>
		<comments>http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/#comments</comments>
		<pubDate>Tue, 11 Mar 2008 14:28:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Annotations]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Marker Interface]]></category>
		<category><![CDATA[Serializable]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/</guid>
		<description><![CDATA[A marker interface is a design pattern consisting in an interface with no methods declared. All the classes implementing this interface don’t have to implement a method for the interface(since it doesn’t have an interface), but we can say that the classes implementing the interfaces are marked. For example in Java we have the Serializable [...]]]></description>
			<content:encoded><![CDATA[<p>A marker interface is a design pattern consisting in an interface with no methods declared. All the classes implementing this interface don’t have to implement a method for the interface(since it doesn’t have an interface), but we can say that the classes implementing the interfaces are marked. For example in Java we have the Serializable interface. It is a Marker Interface, when we create a class in order to mark it as a serializable class, it will implement the serializable, so everyone using it will know that the class is serializable.<br />
<span id="more-11"></span></p>
<p>However there are 2 major drawbacks:<br />
-	All the subclasses implement a marker, and there is not option to unmark a subclass. For example if the base class is marked as Serializable all the subclasses will be marked as Serializable and there is no option to unmark them by “unimplementing” the Interface.<br />
-	It is dangerous to have several levels of markers (Let’s say we need XmlSerializable and BinarySerializable both extending Serializable). What is a class which is not XmlSerializable? Is it BinarySerializable or NotSerializable?</p>
<p>Starting with Java 1.5 annotations  and attributes in .NET can be used instead of Marker Design Pattern. Annotations can be accessed using reflection and can be used to mark not only Classes but also Methods and Attributes.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/" height="61" width="51" /></a></div><h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a href="http://www.factorypattern.com/how-to-simulate-multiple-inheritance-in-java/" title="How to Simulate Multiple Inheritance in Java">How to Simulate Multiple Inheritance in Java</a></li><li><a href="http://www.factorypattern.com/how-to-readwrite-java-properties-files/" title="How to Read/Write Java Properties Files">How to Read/Write Java Properties Files</a></li><li><a href="http://www.factorypattern.com/using-visitor-patterns-on-non-visitable-structures/" title="Using Visitor Patterns on non Visitable Structures">Using Visitor Patterns on non Visitable Structures</a></li><li><a href="http://www.factorypattern.com/multimap-in-google-collections-library/" title="Multimap in Google Collections Library">Multimap in Google Collections Library</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/drawbacks-of-marker-interface-design-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.648 seconds -->
