Thursday 4 October 2018

                                  Factory Design Pattern in java


Factory Pattern is one of the Creational Design pattern and it’s widely used in JDK 
as well as frameworks like Spring and Struts.

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. 
This pattern take out the responsibility of instantiation of a class from client program to the factory class.


Factory Design Pattern Advantages

1. Factory design pattern provides approach to code for interface rather than implementation.

2. Factory pattern removes the instantiation of actual implementation classes from client code. 

3.Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.

4. Factory pattern provides abstraction between implementation and client classes through inheritance.

Factory Design Pattern Examples in JDK
java.util.Calendar, ResourceBundle and NumberFormat getInstance() methods uses Factory pattern. valueOf() method in wrapper classes like Boolean, Integer etc.

Main goal of Factory design pattern:----------------------------------
Abstraction
Loosely coupled
Removes the burden from client
Easy to extend







//interface Computer

package com.govind.beans;

public interface Computer
{
   public String getRam();
 
   public String getCpu();
 
   public String getHdd();
 
 
}


=============================================================================================================

package com.govind.beans;
//class PC .java

public class PC implements Computer
{

private String ram;
private String hdd;
private String cpu;


public PC(String ram, String hdd, String cpu) {
super();
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}

@Override
public String getRam() {
return this.ram;
}

@Override
public String getCpu() {
return this.cpu;
}

@Override
public String getHdd() {
return this.hdd;
}

}


====================================================================================================================================
//class Server.java
package com.govind.beans;

public class Server implements Computer {
private String ram;
private String hdd;
private String cpu;


public Server(String ram, String hdd, String cpu) {
super();
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}

@Override
public String getRam() {
return this.ram;
}

@Override
public String getCpu() {
return this.cpu;
}

@Override
public String getHdd() {
return this.hdd;
}

}


===================================================================================================================================

package com.govind.beans;

public class SuperComputer implements Computer {

private String ram;
private String hdd;
private String cpu;


public SuperComputer(String ram, String hdd, String cpu) {
super();
this.ram = ram;
this.hdd = hdd;
this.cpu = cpu;
}

@Override
public String getRam() {
return this.ram;
}

@Override
public String getCpu() {
return this.cpu;
}

@Override
public String getHdd() {
return this.hdd;
}
}


===========================================================================================================================================
//enum ComputerType.java

package com.govind.factory;

public enum ComputerType {
PC,SERVER,SUPERCOMPUTER;

}


============================================================================================================================================
//class ComputerFactory .java
package com.govind.factory;
import com.govind.beans.Computer;
import com.govind.beans.PC;
import com.govind.beans.Server;
import com.govind.beans.SuperComputer;

public class ComputerFactory {


public static Computer createComputer(ComputerType type,String ram,String hdd,String cpu)
{
Computer comp=null;
switch(type)
{
case PC:
comp=new PC(ram, hdd, cpu);
break;
case SERVER:
    comp=new Server(ram, hdd, cpu);
    break;
    case SUPERCOMPUTER:
    comp=new SuperComputer(ram, hdd, cpu);
    break;
}
return comp;
}
}


===========================================================================================================================================

package com.govind.clientmainprog;
import com.govind.beans.Computer;
import com.govind.factory.ComputerFactory;
import com.govind.factory.ComputerType;

public class ComputerClientProgram {

public static void main(String[] args) { Computer pc = ComputerFactory.createComputer(ComputerType.PC, "2gb", " 500gb", "i3 processor");
Computer server = ComputerFactory.createComputer(ComputerType.SERVER, "8gb", "2tb", "i7 processor");
 Computer supercomputer = ComputerFactory.createComputer(ComputerType.SUPERCOMPUTER, "50gb", "50tb", "i10 processor");
       System.out.println(pc.getRam()+" "+pc.getHdd()+" "+pc.getCpu());
       System.out.println(server.getRam()+" "+server.getHdd()+" "+server.getCpu());
       System.out.println(supercomputer.getRam()+" "+supercomputer.getHdd()+"                     "+supercomputer.getCpu());
   

}

}

================================================================================================================================================


o/p:----------------------------------------------------------------------


2gb  500gb i3 processor
8gb 2tb i7 processor
50gb 50tb i10 processor

No comments:

Post a Comment