Creational Design Pattern: Factory Design Pattern
Creational Design Pattern : Designing pattern related to creation of a object or instance of Class.
1) Factory Design Pattern: When we have a super class which have many sub class and we have to return instance of a sub class according to input.
Following are the main part of factory design pattern:
# super class: super class may be interface , abstract class or a normal java class.
# sub classes:
# factory class:
# super class:
public abstract class Computer {
public abstract String getRAM();
public abstract String getHDD();
public abstract String getCPU();
@Override
public String toString(){
return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+",
CPU="+this.getCPU();
}
}
# sub classes:
Let’s say we have two sub-classes PC and Server with below
implementation.
public class PC extends Computer {
private String ram;
private String hdd;
private String cpu;
public PC(String ram, String hdd, String cpu){
this.ram=ram;
this.hdd=hdd;
this.cpu=cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
public class Server extends Computer {
private String ram;
private String hdd;
private String cpu;
public Server(String ram, String hdd, String cpu){
this.ram=ram;
this.hdd=hdd;
this.cpu=cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
# Factory Class
Now that we have super classes and sub-classes ready, we can write our
factory class. Here is the basic implementation.
package com.journaldev.design.factory;
import com.journaldev.design.model.Computer;
import com.journaldev.design.model.PC;
import com.journaldev.design.model.Server;
public class ComputerFactory {
public static Computer getComputer(String type, String ram, String
hdd, String cpu){
if("PC".equalsIgnoreCase(type)) return new PC(ram, hdd, cpu);
else if("Server".equalsIgnoreCase(type)) return new Server(ram,
hdd, cpu);
return null;
}
}
public class TestFactory {
public static void main(String[] args) {
Computer pc = ComputerFactory.getComputer("pc","2 GB","500
GB","2.4 GHz");
Computer server = ComputerFactory.getComputer("server","16
GB","1 TB","2.9 GHz");
System.out.println("Factory PC Config::"+pc);
System.out.println("Factory Server Config::"+server);
}
Note: Use in local to get language of a country, to get instance of class (getInstance method )
1) Factory Design Pattern: When we have a super class which have many sub class and we have to return instance of a sub class according to input.
Following are the main part of factory design pattern:
# super class: super class may be interface , abstract class or a normal java class.
# sub classes:
# factory class:
# super class:
public abstract class Computer {
public abstract String getRAM();
public abstract String getHDD();
public abstract String getCPU();
@Override
public String toString(){
return "RAM= "+this.getRAM()+", HDD="+this.getHDD()+",
CPU="+this.getCPU();
}
}
# sub classes:
Let’s say we have two sub-classes PC and Server with below
implementation.
public class PC extends Computer {
private String ram;
private String hdd;
private String cpu;
public PC(String ram, String hdd, String cpu){
this.ram=ram;
this.hdd=hdd;
this.cpu=cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
public class Server extends Computer {
private String ram;
private String hdd;
private String cpu;
public Server(String ram, String hdd, String cpu){
this.ram=ram;
this.hdd=hdd;
this.cpu=cpu;
}
@Override
public String getRAM() {
return this.ram;
}
@Override
public String getHDD() {
return this.hdd;
}
@Override
public String getCPU() {
return this.cpu;
}
}
# Factory Class
Now that we have super classes and sub-classes ready, we can write our
factory class. Here is the basic implementation.
package com.journaldev.design.factory;
import com.journaldev.design.model.Computer;
import com.journaldev.design.model.PC;
import com.journaldev.design.model.Server;
public class ComputerFactory {
public static Computer getComputer(String type, String ram, String
hdd, String cpu){
if("PC".equalsIgnoreCase(type)) return new PC(ram, hdd, cpu);
else if("Server".equalsIgnoreCase(type)) return new Server(ram,
hdd, cpu);
return null;
}
}
public class TestFactory {
public static void main(String[] args) {
Computer pc = ComputerFactory.getComputer("pc","2 GB","500
GB","2.4 GHz");
Computer server = ComputerFactory.getComputer("server","16
GB","1 TB","2.9 GHz");
System.out.println("Factory PC Config::"+pc);
System.out.println("Factory Server Config::"+server);
}
Note: Use in local to get language of a country, to get instance of class (getInstance method )
Comments
Post a Comment