Skip to main content

Creational Design Patterns for Beginners

Photo by Christopher Gower on Unsplash


Creational design patterns in Object-Oriented Programming (OOP) deal with the mechanisms of object creation, aiming to create objects in a manner suitable to the situation. This approach offers flexibility, allowing for more efficient and scalable designs.

In this guide, we will cover the following creational design patterns:

  • Singleton
  • Factory Method
  • Abstract Factory
  • Builder
  • Prototype

Each pattern will include an easy-to-understand Kotlin example, which you can run using the Kotlin Playground.

Singleton Pattern

The Singleton pattern ensures that a class has only one instance and provides a global access point to that instance.

Example

object DatabaseConnection {
init {
println("Database Connection created!")
}

fun connect() {
println("Connected to the database.")
}
}

fun main() {
val connection1 = DatabaseConnection
connection1.connect()

val connection2 = DatabaseConnection
connection2.connect()

println("Are connection1 and connection2 the same instance? ${connection1 === connection2}")
}

Factory Method Pattern

The Factory Method pattern allows subclasses to decide which class to instantiate. This pattern defines a method that should be used for object creation, letting subclasses modify the type of objects created.

Example

abstract class Transport {
abstract fun deliver(): String
}

class Truck : Transport() {
override fun deliver(): String {
return "Deliver by Truck"
}
}

class Ship : Transport() {
override fun deliver(): String {
return "Deliver by Ship"
}
}

abstract class Logistics {
abstract fun createTransport(): Transport

fun planDelivery(): String {
val transport = createTransport()
return transport.deliver()
}
}

class RoadLogistics : Logistics() {
override fun createTransport(): Transport {
return Truck()
}
}

class SeaLogistics : Logistics() {
override fun createTransport(): Transport {
return Ship()
}
}

fun main() {
val roadLogistics = RoadLogistics()
println(roadLogistics.planDelivery())

val seaLogistics = SeaLogistics()
println(seaLogistics.planDelivery())
}

Abstract Factory Pattern

The Abstract Factory pattern is used to create families of related or dependent objects. It provides an interface for creating objects, without specifying their exact classes.

Example

interface GUIFactory {
fun createButton(): Button
fun createCheckbox(): Checkbox
}

interface Button {
fun click(): String
}

interface Checkbox {
fun check(): String
}

class WindowsButton : Button {
override fun click(): String = "Windows Button clicked"
}

class MacButton : Button {
override fun click(): String = "Mac Button clicked"
}

class WindowsCheckbox : Checkbox {
override fun check(): String = "Windows Checkbox checked"
}

class MacCheckbox : Checkbox {
override fun check(): String = "Mac Checkbox checked"
}

class WindowsFactory : GUIFactory {
override fun createButton(): Button = WindowsButton()
override fun createCheckbox(): Checkbox = WindowsCheckbox()
}

class MacFactory : GUIFactory {
override fun createButton(): Button = MacButton()
override fun createCheckbox(): Checkbox = MacCheckbox()
}

fun main() {
val windowsFactory: GUIFactory = WindowsFactory()
val macFactory: GUIFactory = MacFactory()

val windowsButton = windowsFactory.createButton()
println(windowsButton.click())

val macCheckbox = macFactory.createCheckbox()
println(macCheckbox.check())
}

Builder Pattern

The Builder pattern allows for step-by-step object construction, especially useful for creating complex objects without overloading constructors.

Example

class House private constructor(
val windows: Int,
val doors: Int,
val hasGarage: Boolean,
val hasSwimmingPool: Boolean
) {
data class Builder(
var windows: Int = 0,
var doors: Int = 0,
var hasGarage: Boolean = false,
var hasSwimmingPool: Boolean = false
) {
fun windows(windows: Int) = apply { this.windows = windows }
fun doors(doors: Int) = apply { this.doors = doors }
fun garage(hasGarage: Boolean) = apply { this.hasGarage = hasGarage }
fun swimmingPool(hasSwimmingPool: Boolean) = apply { this.hasSwimmingPool = hasSwimmingPool }

fun build() = House(windows, doors, hasGarage, hasSwimmingPool)
}

override fun toString(): String {
return "House(windows=$windows, doors=$doors, hasGarage=$hasGarage, hasSwimmingPool=$hasSwimmingPool)"
}
}

fun main() {
val house = House.Builder()
.windows(10)
.doors(5)
.garage(true)
.swimmingPool(true)
.build()
println(house)
}

Prototype Pattern

The Prototype pattern is used to create duplicates (clones) of an object. This pattern is useful when creating an object from scratch is costly, and a similar object is available that can be copied.

Example

interface Prototype<T> {
fun clone(): T
}

data class Sheep(val name: String, val weight: Int) : Prototype<Sheep> {
override fun clone(): Sheep {
return Sheep(name, weight)
}
}

fun main() {
val originalSheep = Sheep("Dolly", 80)
val clonedSheep = originalSheep.clone()

println("Original: $originalSheep")
println("Cloned: $clonedSheep")
}

Conclusion

In this guide, we covered five important Creational Design Patterns:

  1. Singleton: Ensures a class has only one instance.
  2. Factory Method: Allows subclasses to decide which class to instantiate.
  3. Abstract Factory: Creates families of related or dependent objects.
  4. Builder: Simplifies the construction of complex objects.
  5. Prototype: Creates duplicates of an object.

By understanding these patterns, you can gain more control over how your objects are created and make your code more flexible and maintainable. Try experimenting with the provided examples in a Kotlin playground to see how these patterns work in practice.

Comments

Popular posts from this blog

Understanding Number Systems: Decimal, Binary, and Hexadecimal

In everyday life, we use numbers all the time, whether for counting, telling time, or handling money. The number system we’re most familiar with is the   decimal system , but computers use other systems, such as   binary   and   hexadecimal . Let’s break down these number systems to understand how they work. What is a Number System? A number system is a way of representing numbers using a set of symbols and rules. The most common number systems are: Decimal (Base 10) Binary (Base 2) Hexadecimal (Base 16) Each system has a different “base” that tells us how many unique digits (symbols) are used to represent numbers. Decimal Number System (Base 10) This is the system we use daily. It has  10 digits , ranging from  0 to 9 . Example: The number  529  in decimal means: 5 × 1⁰² + 2 × 1⁰¹ + 9 × 1⁰⁰ =  500 + 20 + 9 = 529 Each position represents a power of 10, starting from the rightmost digit. Why Base 10? Decimal is base 10 because it has 10 digits...

How to Monetize Your API as an Individual Developer While Hosting on Your Own Server?

In the API economy, cloud services like AWS, Google Cloud, and Azure offer many conveniences, such as scaling and infrastructure management. However, some developers prefer more control and autonomy, opting to host their APIs on personal servers. Whether for cost efficiency, data privacy, or customization, hosting your own API comes with both advantages and challenges. But, even without cloud platforms, there are effective ways to monetize your API. This guide will explore how individual developers can successfully monetize their APIs while hosting them on their own servers. Why Host Your API on Your Own Server? Hosting your own API gives you full control over the infrastructure and potentially lower long-term costs. Here’s why some developers choose this approach: Cost Control : Instead of paying ongoing cloud fees, you may opt for a one-time or lower-cost hosting solution that fits your budget and resource needs. Data Ownership : You have full control over data, which is critical if ...

API Testing with Jest and Supertest: A Step-by-Step Guide

API testing is essential to ensure your endpoints behave as expected across all scenarios. In this guide, we’ll explore how to use Jest and Supertest to test a sample API with various response types, including success, authentication errors, and validation errors. By the end, you’ll understand how to apply these tools to check for different response structures and status codes. 0. Prerequisites: Setting Up Your Environment Before diving into API testing, it’s important to ensure that your development environment is properly set up. Here’s what you need to do: Step 1: Install Node.js and npm Node.js  is a JavaScript runtime that allows you to run JavaScript code on the server side. It comes with  npm  (Node Package Manager), which helps you install and manage packages. Installation Steps: Download and install Node.js from the  official website . To verify the installation, open your terminal and run: node -v npm -v This should display the installed versions of Node.js...

The Weight of Responsibility: A Developer’s Journey to Balance Passion and Reality

For the past several years, Eddie has been on a steady climb in his career as a developer, but recently, he found himself at a crossroads — caught between the weight of his responsibilities and the desire to pursue his true passions. His journey began with a three-month internship as a web developer, which led to nearly four years in an application developer role. After that, he spent almost a year as a systems associate, managing tasks across systems analysis, quality assurance, and business analysis. Eventually, he returned to full-time software development for another two years before transitioning into more complex roles. For over a year, he worked as a multi-role software developer and database administrator before stepping into his current position as a senior software developer, database administrator, and cloud administrator — occasionally handling security tasks as well. Now, with over 8 years of professional experience, he also leads a small team of developers, which has been...

Avoiding Confusion in API Design: The Importance of Clear Responses

In today’s fast-paced software development landscape, APIs play a crucial role in connecting services and enabling functionality. However, poor design choices can lead to confusion and inefficiency for both developers and users. One such choice is the omission of a response body for successful requests, a practice I recently encountered in an enterprise API designed for bill payments. The Case of the No-Response API The API in question serves two main endpoints: one for inquiring about account validity and another for confirming payment. When successful, the API returned a  200 OK  status but no response body. This design choice led to significant confusion during our integration process. Even the internal team who developed the said API struggled to justify this approach, revealing a lack of clarity around the rationale behind it. Pros of This Design Choice While the intention behind this design may have been to streamline responses, several potential benefits can be identifi...