DP is a well-described solution to a common software problem. Its benefits:
- Already defined to solve a problem.
- Increase code reusability and robustness.
- Faster devlopment and new developers in team can understand it easily
DP defined in to 3 categories:
- Creational - Used to construct objects such that they can be decoupled from their implementing system.
- Structural - Used to form large object structures between many disparate objects
- Behavioral - Used to manage algorithms, relationships, and responsibilities between objects.
- Factory - Exposes a method for creating objects, allowing sub-classes to control the actual creation process.
Consider a scenario as below:-
- You have an interface or abstract class or a normal class. Computer class
- There are 2 sub-classes of of Computer. PC class and Server class
- Now you have a factory which provides you Computers. And you can give specification to factory whether you want a PC or Server.
Below code implements above scenario:
package
com.test.command.dp.creational.factory;
/**Abstract 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();
}
}
|
package
com.test.command.dp.creational.factory;
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() {
// TODO Auto-generated
method stub
return this.ram;
}
@Override
public String getHDD() {
// TODO Auto-generated
method stub
return this.hdd;
}
@Override
public String getCPU() {
// TODO Auto-generated
method stub
return this.cpu;
}
}
|
package
com.test.command.dp.creational.factory;
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() {
// TODO Auto-generated
method stub
return this.ram;
}
@Override
public String getHDD() {
// TODO Auto-generated
method stub
return this.hdd;
}
@Override
public String getCPU() {
// TODO Auto-generated
method stub
return this.cpu;
}
}
|
package
com.test.command.dp.creational.factory;
/**Factory**/
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 static void main(String[]
args) {
Computer pc = ComputerFactory.getComputer("PC", null, null, null);
Computer server =
ComputerFactory.getComputer("SERVER", "12", null, null);
System.out.println("Your PC Dinesh Sachdev. Delivered to Indore:"+pc);
System.out.println("Your Server Dinesh Sachdev. Delivered to Indore:"+server);
}
}
|
Comments
Post a Comment