Design Pattern-Factory Pattern

Factory method pattern A pattern for creating objects, which is widely used in jdk and Spring and Struts frameworks;
The factory method pattern is based on "input" and is applied between the superclass and multiple subclasses. This model transfers the responsibility of creating objects to the factory class;
First let's learn how to apply the factory method pattern in Java and learn the advantages of the factory method. In addition, the factory method pattern is also widely used in jdk;
The superclass can be an interface, an abstract class, or a parent class. In this example, we will override the toString () method to explain the factory method pattern

Instance package com.journaldev.design.model;

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

Factory design pattern subclass
Assume that the subclasses PC and Server implement Computer:

Instance package com.journaldev.design.model;

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

Server also implements Computer:

Instance package com.journaldev.design.model;

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 you have multiple subclasses and superclasses, you can create factory classes:

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

It is important to point out that:

The factory class can be singleton, and getComputer can be static;

getComputer is a method of the factory class and returns different objects based on the same parameter type;

What follows is a simple test client that implements the factory design pattern above.

Instance package com.journaldev.design.test;

import com.journaldev.design.abstractfactory.PCFactory;
import com.journaldev.design.abstractfactory.ServerFactory;
import com.journaldev.design.factory.ComputerFactory;
import com.journaldev.design.model.Computer;

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

}

Output:

Factory PC Config :: RAM = 2 GB, HDD = 500 GB, CPU = 2.4 GHz
Factory Server Config :: RAM = 16 GB, HDD = 1 TB, CPU = 2.9 GHz

Advantages of factory design patterns

Interface-oriented programming embodies the object-oriented thinking;
Transferred the work of creating objects to the factory class;

Example of factory design pattern in JDK

java.util.Calendar, ResourceBundle and NumberFormat getInstance () uses the factory method pattern;
valueOf () in the wrapper class, such as Boolean, Integer also uses the factory method pattern;

Author: