Factory pattern under creational design pattern is widely used design pattern in our software development.To make code reusable,make significant meaning of code,reduce dependency of instantiating Objects from client side its a popular design pattern.
By using this pattern we can give a common interface to user to create a object.If object creation code is spread in whole application then this code need to re-factoring and will more difficult to reuse this kind of code. The basic principle behind this pattern is that,at run time, we get an object of similar type based on the parameter we pass.
Factory Design pattern widely used in different Open source projects like Spring,struts,Apache and also Our jdk use this factory design pattern.
We are going to implement this code like this...
Factory pattern needs this components...
1. An interface/abstract class that will be implemented among similier classes.
2. An Object Factory from where we will get an instance of an object that will choose by the client.
3. The sub-classes that will implement the interface or extends abstract classes.
Interface::
Object factory that will return the objects...
Then the sub-classes that will implement base interface.
Here we can choose any design pattern for my project implementation.But still now I need a enum or constant class to specify my choose.
Now we can choose a pattern by implementing our main class
And you can get the project from Factory Pattern
Happy Coding...:P
By using this pattern we can give a common interface to user to create a object.If object creation code is spread in whole application then this code need to re-factoring and will more difficult to reuse this kind of code. The basic principle behind this pattern is that,at run time, we get an object of similar type based on the parameter we pass.
Factory Design pattern widely used in different Open source projects like Spring,struts,Apache and also Our jdk use this factory design pattern.
We are going to implement this code like this...
Factory pattern needs this components...
1. An interface/abstract class that will be implemented among similier classes.
2. An Object Factory from where we will get an instance of an object that will choose by the client.
3. The sub-classes that will implement the interface or extends abstract classes.
Interface::
package factory; /** * Created by Flop Coder on 7/10/14. */ public interface DesignTest { public void test(); }
Object factory that will return the objects...
package factory; import patterns.abstract_factory.AbstractFactoryTest; import constants.Constant; import patterns.prototype.PrototypeTest; import patterns.singletone.SingletonTest; /** * Created by Flop Coder on 7/10/14. */ public class ObjectFactory { public static DesignTest testDesignPattern(String nameOfPattern) { if (nameOfPattern.equals(Constant.SINGLETON)) return new SingletonTest(); else if (nameOfPattern.equals(Constant.PROTOTYPE)) return new PrototypeTest(); else if(nameOfPattern.equals(Constant.ABSTRUCT_FACTORY)) return new AbstractFactoryTest(); return null; } }
Then the sub-classes that will implement base interface.
package patterns.abstract_factory; import factory.DesignTest; /** * Created by Flop Coder on 7/10/14. */ public class AbstractFactoryTest implements DesignTest { @Override public void test() { System.out.println("This is AbstructFactoryTest"); } }
package patterns.prototype; import factory.DesignTest; /** * Created by Flop Coder on 7/10/14. */ public class PrototypeTest implements DesignTest{ @Override public void test() { System.out.println("This Is prototype example"); } }
package patterns.singletone; import factory.DesignTest; /** * Created by Flop Coder on 7/10/14. */ public class SingletonTest implements DesignTest { @Override public void test() { SingleToneSample singleToneSample = SingleToneSample.getSingleToneSample(); SingleToneSample ex2 = SingleToneSample.getSingleToneSample(); } }
package patterns.singletone; /** * Created by Flop Coder on 7/8/14. */ public class SingleToneSample { private static SingleToneSample singleToneSample; private SingleToneSample() { System.out.println("SingleToneSample(): Initializing Instance"); } public static SingleToneSample getSingleToneSample() { if (singleToneSample == null) { synchronized (SingleToneSample.class) { if (singleToneSample == null) { System.out.println("getSingleToneSample() OBJECT WILL CREATE"); singleToneSample = new SingleToneSample(); System.out.println("getSingleToneSample() OBJECT IS CREATED"); } } } System.out.println("INSTANCE IS AVAILABLE... :P"); return singleToneSample; } public void doSomething() { System.out.println("doSomethingc(): Singleton does something!"); } }
Here we can choose any design pattern for my project implementation.But still now I need a enum or constant class to specify my choose.
package constants; /** * Created by Flop Coder on 7/10/14. */ public class Constant { public static final String SINGLETON = "SINGLETON"; public static final String FACTORY = "FACTORY"; public static final String ABSTRUCT_FACTORY = "ABSTRUCT_FACTORY"; public static final String PROTOTYPE = "PROTOTYPE"; }In this Example we can choose any design pattern without knowing internal structure but which type we want to select.
Now we can choose a pattern by implementing our main class
import constants.Constant; import factory.DesignTest; import factory.ObjectFactory; import java.util.logging.Logger; /** * Created by Flop Coder on 7/8/14. */ public class Main { static Logger log = Logger.getLogger(Main.class.getName()); public static void main(String[] args) { DesignTest designTest = ObjectFactory.testDesignPattern(Constant.PROTOTYPE); designTest.test(); } }After that our project structure will like this...
And you can get the project from Factory Pattern
Happy Coding...:P
0 Comments