Abstract Design Pattern In Java With Example
Posted By : Vakeel Malik | 17-Aug-2018
Abstract pattern design is a factory of factory design pattern. an abstract design pattern is used to solved complicated object relationship problems.
Abstract design pattern also solves product families problems without specifying their classes. the abstract design is an interface for all classes which is related to the module.
In Abstract design pattern, an interface is responsible to create the factory for each related objects separately without explicitly specifying their classes.
Abstract design pattern work around the super factory which create other factories. this type of factory is called the factory design pattern.
Now we are going to the implementation of the abstract design pattern. we have created computer class as abstract with three method
1.getRAM()
2. getCPU()
3. getHDD()
Each method is declared as an abstract method with String return type and without method definition. these methods return the capacity of the computer. we have created two classes PC and Server, both classes extend Computer class and implemented all methods of supercomputer class.
We have created an interface as ComputerAbstractFactory with
we have created another factory computer factory of computer class with the staticcreateComputer method and one ComputerAbstractFactory argument.
we have created MainClass with the main and
we called creatComputer method of ComputerFactory class from
Example:-
abstract class Computer {
public abstract String getRAM();
public abstract String getCPU();
public abstract String getHDD();
public String toString()
{
return "RAM"+this.getRAM()+", CPU "+this.getCPU()+", HDD "+this.getHDD();
}
}
class PC extends Computer{
private String RAM;
private String CPU;
private String HDD;
public PC(String RAM, String CPU, String HDD)
{
this.RAM=RAM;
this.CPU=CPU;
this.HDD=HDD;
}
public String getRAM()
{
return this.RAM;
}
public String getCPU()
{
return this.CPU;
}
public String getHDD()
{
return this.HDD;
}
}
class Server extends Computer{
private String CPU;
private String RAM;
private String HDD;
public Server(String CPU,String RAM, String HDD)
{
this.CPU=CPU;
this.RAM=RAM;
this.HDD=HDD;
}
public String getCPU()
{
return this.CPU;
}
public String getRAM()
{
return this.RAM;
}
public String getHDD()
{
return this.HDD;
}
}
interface ComputerAbstrctFactory{
public Computer createComputer();
}
class PCFactory implements ComputerAbstrctFactory {
private String RAM;
private String CPU;
private String HDD;
public PCFactory(String RAM, String CPU, String HDD)
{
this.RAM=RAM;
this.CPU=CPU;
this.HDD=HDD;
}
public Computer createComputer()
{
return new PC(RAM,CPU,HDD);
}
}
class ServerFactory implements ComputerAbstrctFactory {
private String RAM;
private String CPU;
private String HDD;
public ServerFactory(String RAM, String CPU, String HDD)
{
this.RAM=RAM;
this.CPU=CPU;
this.HDD=HDD;
}
public Computer createComputer()
{
return new Server(RAM,CPU,HDD);
}
}
class ComputerFactory {
public static Computer createComputer(ComputerAbstrctFactory factory )
{
return factory.createComputer();
}
}
public class MainClass {
public static void main(String arg[])
{
MainClass.testFactory();
}
static void testFactory()
{
Computer server = ComputerFactory.createComputer(new ServerFactory("26 GB"," 1 TB","2.9 GHz"));
Computer pc = ComputerFactory.createComputer(new PCFactory("10 GB"," 20 TB","2.4 GHz"));
System.out.println("server ="+server);
System.out.println("pc ="+pc);
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Vakeel Malik
Vakeel is a bright Java Developer working in Oodles.