Factory design pattern in Java

Factory Design Pattern in Java is creational design pattern, in this article we will look into this and try to understand the what problem it solves and its practical uses.

Factory Design Pattern
Factory Design Pattern

Introduction

Factory Method is a creational design pattern that provides an interface for creating objects without specifying the exacts class that will be instantiated. In factory method instead of creating the object directly, we use a factory method to create and return the object. It enables us to create objects without knowing the implementation details of our classes.

Factory Method Design Pattern

We create object without exposing the creation logic to the client, this promotes code reuse and loose coupling between classes making our code more modular and easier to maintain.

Factory method Design Pattern Advantages

  • Abstraction: Provide abstraction between the implementation and client classes.
  • Loose coupling: Abstract factory pattern promote the concept of coding with interface rather than implementation, hence promote loose coupling
  • Extensibility: The Abstract Method design pattern can be extended with new feature without modifying the existing code.

Factory Method Design Pattern Disadvantages

  • Complexity and overhead: It require the creation of additional interfaces and additional classes to extend the feature that add more complexity to the code.

Factory Method pattern use case

  • to encapsulate the object creation process.
  • to promote loose coupling between classes.
  • to promote code reuse.

Creating Factory design factory

  1. Parent class in factory design pattern can be an abstract class/ Interface or just a normal class.
package com.javaocean.in.factory;
public abstract class Machine {
        public abstract String getMemory();
        public abstract String getHardisk();
        public abstract String getProcessor();       

        @Override
        public String toString(){
               return "RAM= "+this.getMemory()+", HDD="+this. getHardisk ()+", CPU="+this. getProcessor ();
        }
}

  1. Let we are producing two type of computer machine a) PersonalComputer  b)ServerMachine

 

package com.javaocean.in.factory;
public class PersonalComputer extends Machine {
         private String memory;
        private String hardisk;
        private String processor;
                public PersonalComputer (String memory, String hardisk, String processor){
               this. memory = memory;
               this. hardisk = hardisk;
               this. processor = processor;
        }        
         @Override
        public String getMemory() {
               return this.memory;
        }
         @Override
        public String getHardisk() {
               return this.hardisk;
        }
         @Override
        public String getProcessor() {
               return this.processor;
        }
 }

3. Let’s create ServerMachine by extending the machine parent class.

package com.javaocean.in.factory;
public class ServerMachine extends Machine{
         private String memory;
        private String hardisk;
        private String processor;
                public ServerMachine(String memory, String hardisk, String processor){
               this.memory=memory;
               this.hardisk=hardisk;
               this.processor=processor;
        }
        @Override
        public String getMemory() {
               return this.memory;
        }
         @Override
        public String getHardisk() {
               return this.hardisk;
        }
         @Override
        public String getProcessor() {
               return this.processor;
        }
 }

4. Our super classes and sub-classes ready, now let’s create our factory class.

  package com.javaocean.in.factory;

public class Factory{
         public static Machine getMachine(String type, String memory, String hardisk, String processor){
               if("PersonalComputer".equalsIgnoreCase(type)) return new PersonalComputer(memory, hardisk, processor); 
              else if("ServerMachine".equalsIgnoreCase(type)) return new ServerMachine(memory, hardisk, processor); 
                             return null;
        }
}

5.      Let’s create class to test the factory design pattern

package com.javaocean.in.factory;
 public class JavaOceanFactory {
         public static void main(String[] args) {
               Machine pc = Factory.getMachine("PersonalComputer","8 GB","500 GB","3.4 GHz");
               Machine server = Factory.getMachine("ServerMachine","32 GB","2 TB","3.9 GHz");
               System.out.println("Personal Computer Config:"+pc); 
              System.out.println("Server Machine Config:"+server);
      }
 }

Output of above program is:

Personal Computer Config:RAM= 8 GB, HDD=500 GB, CPU= 3.4 GHz
Server Machine Config:RAM= 32 GB, HDD= 2 TB, CPU= 3.9GHz

Real Life Examples of Factory Design Pattern

Database Connection : Database connection factories are a common example of the Factory pattern. The factory class can examine the parameters (such as database type, credentials, etc.) and create the appropriate database connection object, hiding the specific implementation details from the client.
Logging Systems: Logging frameworks utilize the Factory pattern to create loggers with different configurations. The factory class can take in parameters like log level, log file destination, or log format.

You may find other post on different topic that might be helpful, Please have a look

  1. Singleton design pattern.
  2. Builder design pattern
  3. Abstract class and Interface
  4. Optional in Java
  5. Default and static method in Java

 

 

6 thoughts on “Factory design pattern in Java”

  1. Pingback: - Java Ocean

Leave a Comment