Link Search Menu Expand Document

Creational Design Patterns

This tutorial is about “Creational Design Patterns” which refers to useful ways when creating objects in Object-Oriented Programming with Java.

First, we define creational design patterns, explain what they provide, and then define the most well-known and most commonly used structural models such as Abstract Factory, Factory Method, Builder, Prototype, Singleton. Finally, we summarize the tutorial.

What does Creational Design Pattern mean?

Creational design patterns provide general suggestions on how to create software objects. Thanks to the flexible structure they use we can create the necessary objects based on predetermined situations.

Creational patterns allow the system to call the appropriate object without tracking which object should be called. They add noticeable flexibility to the application when we need to create objects. The main purpose is that the objects in good software should be designed independently of how they were created.

Creational design patterns concern the objects that need to be created in the working process of the application. 5 different patterns anticipate the creation of the objects within certain structures.

Abstract Factory

This pattern has importance in designs related to production. Relationships such as factory and product groups are defined once. We have to ensure that products that are structurally similar to each other can be managed through a common middle layer in this kind of structure.

The setup in this way provides an easier and more flexible design. It allows the creation of a family of objects on different platforms using a single interface. In other words, the application can be moved to different platforms without changing behavior.

Factory Method

It basically separates the functions of object creation from the interface by inheriting how the object is created by subclasses and using a single interface for object creation.

Let's create a Car interface. Hatchback and Sedan will be one type of our Car. Since these models will be connected to the car, we will implement our car interface.

public interface  Car {
public abstract String getType();
}
public class Hatchback implements Car{
 
public String getTip() {
return "Hatchback Type Car Selected.";
}
}
 
public class Sedan implements Car{
public String getTip() {
return "Sedan Type Car Selected.";
}
}

Whenever the user selects a vehicle type, that vehicle type object must be created and then continue the process. We will create a "Factory" for this.

public class CarFactory {
public static Araba getCar(String tip){
if("Sedan".equalsIgnoreCase(tip)) return new Sedan();
else if("Hatchback".equalsIgnoreCase(tip)) return new Hatchback();
return null;
}
}

Now it's time to run our program. Let's create our main class. We requested two cars from our program, and these cars were produced from the Car object and sent to CarFactory to create the relevant object.

public class MainFactory {
public static void main(String[] args) {
Car hatchbackCar = CarFactory.getCar("Hatchback");
Car sedanCar = CarFactory.getCar("Sedan");
System.out.println(hatchbackCar.getType());
System.out.println(sedanCar.getType());
}
}

As a result, there should be an image on the screen.

  Hatchback Type Car Selected.

Sedan Type Car Selected.

Builder

Allows us to create parts as needed from a complex group of objects using a single interface. When we use the object group, we structure it in the desired way and we do not create unnecessarily unused parts so that resource expenditure is eliminated.

Prototype

It offers a structure that acts as a prototype for the objects to be created. In other words, when creating objects from classes, it ensures that new objects are not created from scratch, but by accepting the existing ones as examples. Thanks to this pattern, objects are created without spending too many resources.

Singleton

This pattern can be used effectively when a mechanism that requires the creation of the object only once is desired. It provides the ability to make a constraint that only one object can be created from a class, and the object is not created until it is needed for the first time.

Summary

In this tutorial, we define creational patterns first. Then we explain some mostly used creational patterns in the software world.

The first one is the “Abstract Factory” which provides the production of multiple relational objects by defining a different interface for each product family. After that, we mention “Factory Method” which is a structure based on creating a class.

Then we describe the “Builder” pattern that enables the creation of a complex group of objects piece by piece, through a single interface. In this pattern used object groups are structured in the required direction. Unused parts are created unnecessarily and do not waste resources.

Next, we mention the “Prototype” pattern that enables us to create new objects by sampling from existing ones, rather than creating them from scratch when we create objects from complex and/or expensive classes. In this way, new objects are created easily and without using resources unnecessarily.

Finally, we explain the “Singleton” pattern that provides a constraint that only one object can be created from a class. This object can be accessed from anywhere in the application and may not be created until the first use.

Other useful articles:


Back to top

© , Java Interviews — All Rights Reserved - Terms of Use - Privacy Policy