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 the createComputer method, which will return the reference object of computer class. we have created two factories ComputerFctory and ServerFactory, which implements createComputer method of ComputerAbstractFactory interface. 
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 
testFactory methods. testfactory method called from the main method.
we called creatComputer method of ComputerFactory class from
the testfactory method of MainClass. 

 

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);
  }
 
}


About Author

Author Image
Vakeel Malik

Vakeel is a bright Java Developer working in Oodles.

Request for Proposal

Name is required

Comment is required

Sending message..