<?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; Object Oriented Design</title>
	<atom:link href="http://www.factorypattern.com/category/object-oriented-design/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>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>Google Guice Tutorial</title>
		<link>http://www.factorypattern.com/google-guice-tutorial/</link>
		<comments>http://www.factorypattern.com/google-guice-tutorial/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 19:49:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java Frameworks]]></category>
		<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Dependency Inversion]]></category>
		<category><![CDATA[Injector]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.factorypattern.com/google-guice-tutorial/</guid>
		<description><![CDATA[What is Google Guice
Google Guice is a light java dependency inversion framework using annotations. It is developed by Google(Bob Lee and Kevin Bourrillion) and it is used internally by Google for their applications. Google Guice is sometimes considered an Inversion of Control Container but as their authors state it’s not a container, it’s just an [...]]]></description>
			<content:encoded><![CDATA[<p><H2>What is Google Guice</H2></p>
<p>Google Guice is a light java dependency inversion framework using annotations. It is developed by Google(<a href="http://crazybob.org/">Bob Lee</a> and <a href="http://smallwig.blogspot.com/">Kevin Bourrillion</a>) and it is used internally by Google for their applications. Google Guice is sometimes considered an Inversion of Control Container but as their authors state it’s not a container, it’s just an Dependency Injector, being too<span id="more-20"></span> light to be considered a container. Beside Dependency injection Google Guice does some more AOP stuff. However in this tutorial we are going to talk only about the Dependency Injection part. In order to use Google Guice <a href="http://google-guice.googlecode.com/files/guice-1.0.zip">download</a> it and add the jar from the downloaded zip to the classpath of your application.</p>
<p>First of all let’s take a look on what Google Guice does, then we’ll check how it’s doing it. Let’s take the following sample composed of a few classes:</p>
<p><strong>Customer</strong> – contains the customer data – email address, …<br />
<strong>Notifier</strong> – the interface used to notify a recipient. The customer object contains a reference to the Notifier object.<br />
<strong>SendMail</strong> – an implementation of the Notify interface which notifies a client about the occurred changes by sending an email.<br />
<strong>SendSMS</strong> &#8211; another implementation of the Notify interface notifying a client by sending an SMS.</p>
<p>Let’s see the code when we don’t use any dependency injection framework:</p>
<pre>public class Customer {
      private Notifier notifier;

      public Notifier getNotifier() {
            return notifier;
      }

      public void setNotifier(Notifier notifier) {
            this.notifier = notifier;
      }

      public void changeSomething(){
            this.notifier.sendNotification("This Customer");
      }
}

public interface Notifier {

      public void sendNotification(String to);

}

public class SendMail implements Notifier{

      public void sendNotification(String to){

            System.out.println("Notifying " + to);
      }
}

public class SendSMS implements Notifier{
      public void sendNotification(String to){
            System.out.println("Notifying " + to);
      }
}

public class Main {

      public static void main(String[] args) {

            Customer customer = new Customer();
            Notifier notifier = new SendMail();
            customer.setNotifier(notifier);
            customer.changeSomething();
      }
}</pre>
<h2>Using Google Guice injector with default bindings defined through annotations.</h2>
<p>Now lets use Guice as a dependency injector. First of all we can communicate to Guice the default implementation for the Notifier interface, by using annotations:</p>
<pre>@ImplementedBy(SendMail.class)
public interface Notifier {
    public void sendNotification(String to);
}</pre>
<p>Then we have to change the “application” code. Instead of using new keyword to create a new object we use the getInstance method of the injector Guice creates for us:</p>
<pre>public static void main(String[] args) {
    Injector injector = Guice.createInjector();
    Customer customer = injector.getInstance(Customer.class);
    customer.changeSomething(); // the customer must be notified about the change
}</pre>
<p>Then the last step, we must to tell Guice what to inject in the Customer object. We have here 3 options: Field Injection, Method Injection, Constructor Injection:</p>
<p>1. <strong>Field Injector</strong>  &#8211; Guice will create an object of type Notifier (Guice already knows from the first step that the default implementation for Notifier is SendMail) and inject it in the field “Notifier” of the Customer object. Guice is using reflection here and the interesting part here is that it can inject into private and protected fields as well, breaking the encapsulation:</p>
<pre>@Inject
private Notifier notifier;</pre>
<p>2. <strong>Method Injector</strong>:</p>
<pre>@Inject
public void setNotifier(Notifier notifier) {
    this.notifier = notifier;
}</pre>
<p>3. <strong>Constructor Injector</strong>:</p>
<pre>public class Customer {
…
      @Inject
      public Customer(Notifier notifier) {
            this.notifier = notifier;
      }
…
}</pre>
<p><H2>Defining new bindings</H2></p>
<p>Well done until now, it seems we have everything in place, but what happens if we want to add another notifier class and change the default behavior? Of course we don&#8217;t want to change the annotations in the interfaces changing the default implementation. What we need is to define new bindings. To do this Guice provide a mechanism which assume the creation of another class called Module, that overwrite the bindings. Let&#8217;s say we have another Notifier class, SendSMS and we want to use it. We need to create a module class:</p>
<pre>public class MyModule implements Module{

    public void configure(Binder binder) {
        binder.bind(Notifier.class).to(SendSMS.class);
    }
}</pre>
<p>Now we have the class where we defined the bindings. All we have to do is to tell to the injector to use those bindings. This is how the main method looks:</p>
<pre>public static void main(String[] args) {
    MyModule module = new  MyModule();
    Injector anotherInjector = Guice.createInjector(module);

    Customer customer = anotherInjector.getInstance(Customer.class);
    customer.changeSomething();
}</pre>
<p><H2>Conclusion</H2></p>
<p>We&#8217;ve seen 3 main Google Guice classes in action in this tutorial. Google Guice mainly consists of those classes along with a few other classes which we haven&#8217;t seen in action yet:<br />
<strong>Binder</strong>, <strong>Binding</strong> &#8211; classes responsible for keeping the mapping between interfaces and the implementations that has to be used.<br />
<strong>Injector</strong> &#8211; Fulfills requests for the object instances that make up your application, always ensuring that these instances are properly injected before they are returned.<br />
<strong>Module</strong> &#8211; defines sets of bindings. Modules classes can be used to create injectors (passing a reference to the injector constructor) so the injector uses the binding defined in this.<br />
<strong>Provider</strong> &#8211; provides instances of the applications and can encapsulate some logic to provide one type of implementation or another depending on a context<br />
<strong>Scope</strong> &#8211; helps defining the scope of different injected instances. By scoping Guice offer the option to reuse object instances inside a defined scope (for example reusing objects inside the scope of a session).</p>
<p>In a next part of the tutorial I&#8217;ll try to check in more depth what Google Guice is capable of.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/google-guice-tutorial/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/google-guice-tutorial/" 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/guice-servlets/" title="Guice Servlets Integration">Guice Servlets Integration</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/google-guice-starts-a-new-google-age/" title="Google Guice Starts a New Google Age?">Google Guice Starts a New Google Age?</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/google-guice-tutorial/feed/</wfw:commentRss>
		<slash:comments>8</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>FactoryPattern.com &#8211; on air</title>
		<link>http://www.factorypattern.com/factory-pattern/</link>
		<comments>http://www.factorypattern.com/factory-pattern/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 22:04:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Object Oriented Design]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://factorypattern.com/?p=1</guid>
		<description><![CDATA[What is Factory Pattern?
Factory Pattern is a creational design pattern. It is responsible for creating instances of objects without using directly the object constructor. For this reason Factory Pattern is widely used in frameworks, to encapsulate the logic of creating the objects and to make the framework independent of the objects it needs to work [...]]]></description>
			<content:encoded><![CDATA[<p>What is Factory Pattern?</p>
<p><strong>Factory Pattern</strong> is a creational design pattern. It is responsible for creating instances of objects without using directly the object constructor. For this reason <a href="http://www.oodesign.com/factory-pattern.html">Factory Pattern</a> is widely used in frameworks, to encapsulate the logic of creating the objects and to make the framework independent of the objects it needs to work with.</p>
<p>There are several types of factories, depending of the implementation:</p>
<ul>
<li> Static factories</li>
<li>Factories where the new objects are registered</li>
<li>Factories implemented using Reflection (depending on programming language)</li>
<li>Factory Method &#8211; The Factory Method Design Pattern originally published in the book known as Gang Of Four (<cite class="book" style="font-style: normal"><a href="http://en.wikipedia.org/wiki/Erich_Gamma" title="Erich Gamma">Gamma, Erich</a>; <a href="http://en.wikipedia.org/wiki/Richard_Helm" title="Richard Helm">Helm, Richard</a>; Johnson, Ralph; Vlissides, John (1994). <em><a href="http://en.wikipedia.org/wiki/Design_Patterns" title="Design Patterns">Design Patterns: Elements of Reusable Object-Oriented Software</a></em>. Addison-Wesley )</cite><span class="Z3988" title="ctx_ver=Z39.88-2004&amp;rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&amp;rft.genre=book&amp;rft.btitle=%5B%5BDesign+Patterns%7CDesign+Patterns%3A+Elements+of+Reusable+Object-Oriented+Software%5D%5D&amp;rft.au=%5B%5BErich+Gamma%7CGamma%2C+Erich%5D%5D%3B+%5B%5BRichard+Helm%7CHelm%2C+Richard%5D%5D%3B+Johnson%2C+Ralph%3B+Vlissides%2C+John&amp;rft.pub=Addison-Wesley"></span></li>
</ul>
<p>You can find more information about Factory Design Pattern and the other <a href="http://www.oodesign.com/" title="oodesign.com">Design Patterns</a> on: <a href="http://www.oodesign.com">oodesign.com</a>.</p>
<p><strong>FactoryPattern.com</strong> is  just another blog about the same old fashioned object oriented design and this where the name came of.</p>
<div style="text-align:center;"><a href="http://api.tweetmeme.com/share?url=http://www.factorypattern.com/factory-pattern/"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://www.factorypattern.com/factory-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/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/drawbacks-of-marker-interface-design-pattern/" title="Drawbacks of Marker Interface Design Pattern.">Drawbacks of Marker Interface Design Pattern.</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.factorypattern.com/factory-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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