Factory Method Pattern:
- Problem: Using new keyword we can not create object with flexibility and by applying restrictions.
- Solution: Use Factory pattern (or) Factory method.
- By defining a abstract class or an interface but let the subclass decide which class object to instantiate.
- A method of a class capable of constructing and returning its own class object or other class object is called "factory method".
- There are two types of factory methods.
- Static factory method.
- Instance factory method.
1.Static Factory method:
- A static method defined to construct and return object of same class or different is known as static factory method.
- Some of the pre defined static factory methods are as follows.
- Thread th= Thread.currentThread();
- Class c=Class.forName();
- Runtime rt=Runtime.getRuntime();
- Calendar c=Calendar.getInstance();
2.Instance Factory method:
- A non static method defined to construct and return object of same class or different is known as instance factory method.
- Some of the pre defined instance factory methods are as follows.
- String s= new String("instance of");
String s1=s.concat("java"); - StringBuffer sb=new StringBuffer("instance of");
sb=sb.subString(0,2); - Date d= new Date();
String s=d.toString();
Step 1: create an interface:
- package com.instanceofjavaforus;
- public interface Vehicle {
- public void engine();
- }
Step 2: create a Two Wheeler class which implements vehicle interface:
- package com.instanceofjavaforus;
- public class TwoWheeler implements Vehicle {
- @Override
- public void engine() {
- System.out.println("Two Wheeler");
- }
- }
Step 3:: create a Four Wheeler class which implements vehicle interface:
- package com.instanceofjavaforus;
- public class FourWheeler implements Vehicle {
- @Override
- public void engine() {
- System.out.println("FourWheeler");
- }
- }
Step 4: create a SixWheeler class which implements vehicle interface:
- package com.instanceofjavaforus;
- public class SixWheeler implements Vehicle {
- @Override
- public void engine() {
- System.out.println("SixWheeler");
- }
- }
Step 5: create a TestEngine class which is having factory method:
- package com.instanceofjavaforus;
- public class TestEngine {
- public Vehicle getVehicle(String venname){
- if(venname==null){
- return null;
- }else if(venname.equalsIgnoreCase("TwoWheeler")){
- return new TwoWheeler();
- }else if(venname.equalsIgnoreCase("FourWheeler")){
- return new FourWheeler();
- }else if(venname.equalsIgnoreCase("SixWheeler")){
- return new SixWheeler();
- }
- return null;
- }
- }
Step 6: create a FactoryDemo class:
- package com.instanceofjavaforus;
- public class FactoryDemo {
- public static void main(String args[]){
- TestEngine te= new TestEngine();
- Vehicle vehcle1=te.getVehicle("TwoWheeler");
- vehcle1.engine();
- Vehicle vehcle2=te.getVehicle("FourWheeler");
- vehcle2.engine();
- Vehicle vehcle3=te.getVehicle("SixWheeler");
- vehcle3.engine();
- }
- }
- OutPut:
- TwoWheeler
- FourWheeler
- SixWheeler
No comments