How Spring Uses Factory 🏭 Design Pattern ?

Jahid Momin
3 min readJan 16, 2023

--

Factory Design Pattern — Springboot | Spring
Factory Design Pattern

The Factory design pattern is a creational design pattern that provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created. It uses a factory method to create objects, rather than calling a constructor directly.

Lets assume that , we have a factory which makes some product in separate space/area like this only factory DP states that to create object separately to achieve abstraction / hiding creational details from client code .

The Factory pattern separates the instantiation process from the client code, allowing the client code to be decoupled from the actual objects that are created. This makes the code more flexible and easier to maintain, as the client code doesn’t need to know the details of how the objects are created.

There are two main types of Factory patterns:

  • Simple Factory: This pattern provides a single factory method that creates objects of different types based on the input provided by the client code. This pattern is also known as the Static Factory method pattern.

Make sure that each class implements Same Interface . Animal is implemented by Cat , Dog , Cow Classes.

interface Animal{}

public class Cat implements Animal {}
public class Dog implements Animal {}
public class Cow implements Animal {}

class AnimalFactory {
public static Animal createAnimal(String type) {
switch (type) {
case "cat":
return new Cat();
case "dog":
return new Dog();
case "cow":
return new Cow();
default:
throw new IllegalArgumentException("Invalid Animal type");
}
}
}
//The client code can use this factory method to create different animal types:
Animal cat= AnimalFactory.createAnimal("cat");
Animal dog= AnimalFactory.createAnimal("dog");
Animal cow = AnimalFactory.createAnimal("cow");
  • Factory Method: This pattern provides an interface for creating objects, but allows subclasses to alter the type of objects that will be created. This pattern is also known as the Virtual Constructor pattern.
interface AnimalFactory {
Animal createAnimal();
}

class CatFactory implements AnimalFactory {
public Animal createAnimal() {
return new Cat();
}
}

class DogFactory implements AnimalFactory {
public Animal createAnimal() {
return new Dog();
}
}

class CowFactory implements AnimalFactory {
public Animal createAnimal() {
return new Cow();
}
}

//The client code can use the factory classes to create different animal types:
AnimalFactory catFactory = new CatFactory();
Cat cat = (Cat) catFactory.createAnimal();

AnimalFactory dogFactory = new DogFactory();
Dog dog = (Dog) dogFactory.createAnimal();

AnimalFactory cowFactory = new CowFactory();
Cow cow = (Cow) cowFactory.createAnimal();

Summary , In the first example, the factory method is a static method, that creates the objects based on the input provided. In the second example, the factory method is an interface method and the creation of the object is delegated to the subclasses (Each One Having its Factory Method Separately).

Lets Talk About Spring Framework , It uses the Factory Method pattern to create objects. The Spring IoC container uses reflection to create objects and wire them together based on the configuration(XML/Annotations) provided in the application. It also provides additional functionality such as lifecycle management of objects, aspect-oriented programming, and more.

happy learning , keep following .

--

--

Jahid Momin
Jahid Momin

Written by Jahid Momin

Team Lead | Sr Software Engineer | Spring boot | Microservices | JavaScript | CodeIgniter | HTML | CSS | ReactJS | NextJS | Youtuber | Writer

No responses yet