Tuesday, September 10, 2013

Real time examples of Design patterns being used by Java

I am very keen to learn what all design patterns are being used by java code.

So here I am going to list them one by one.. I'll keep on updating this article as and when I learn more about design patterns...

To begin with.. Lets learn something about strategy design pattern.

Strategy Design Pattern Examples

  • There are common situations when classes differ only in their behavior. For this cases is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime.
    Intent
  • Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
You must have seen how we create threads in Java... We make a class implement Runnable and override its run method. And then use that class's object as an argument to Thread class constructor.
class xx implements Runnable  
{  
       @Override
       public void run()  
       {  
               System.out.println("Running xx thread...");  
       }  
}  
class MyThread extends Thread
{
    @Override
    public void run()
    {
        System.out.println("mythread");
    }
}
public class Main
{ 
    public static void main(String args[])
    {
        xx r = new xx(); 
        // Encapsulate what varies.. Note that 
        // Here the behavior of the run method varies...
        // That is why it has been moved out of the Thread class.. 
        Thread t = new Thread(r);  
        // Now that we have overridden the run method of Runnable interface
        // and passed the object of class implementing it to the constructor of 
        // Thread class...  
        // In this case, the run method of r object will get invoked 
        // by the start method.
        t.start();  

        Thread s = new MyThread();
        // As we have now overriden the method of the Thread
        // class itself, the start method will invoke the overridden
        // run method.
        // Here polymorphysm is attained by inheritance and not by
        // encapsulation.. This is a weaker strategy than the first one.
        s.start();


    }
}
Well, if you see the definition of run() method in Thread class I think you'll agree to what I have tried to explain above... Here is the definition in Thread class
/**
* If this thread was constructed using a separate 
* <code>Runnable</code> run object, then that 
* <code>Runnable</code> object's <code>run</code> method is called; 
* otherwise, this method does nothing and returns. 
* <p>
* Subclasses of <code>Thread</code> should override this method. 
*
* @see     #start()
* @see     #stop()
* @see     #Thread(ThreadGroup, Runnable, String)
*/
public void run() 
{
    if (target != null) 
    {
        target.run();
    }
}
And if you look at the second way of creating a Thread, i.e. extending a Thread class... In that case the start method just invokes the run method... And because we have overridden the run method of Thread class, the default implementation of Thread's run method will not get invoked, instead the implementation that we have given in the child class will get invoked.
/**
     * Causes this thread to begin execution; the Java Virtual Machine 
     * calls the <code>run</code> method of this thread. 
     * <p>
     * The result is that two threads are running concurrently: the 
     * current thread (which returns from the call to the 
     * <code>start</code> method) and the other thread (which executes its 
     * <code>run</code> method). 
     * <p>
     * It is never legal to start a thread more than once.
     * In particular, a thread may not be restarted once it has completed
     * execution.
     *
     * @exception  IllegalThreadStateException  if the thread was already
     *               started.
     * @see        #run()
     * @see        #stop()
     */
    public synchronized void start()
    {
       ..... 
    }
Collections.sort(myCollections, myComparator);
This is another example of strategy design pattern, which I can think of.. As we are passing the behavior of comparison at run-time.

No comments:

Post a Comment