Skip to content

Exploring the Factory Method Pattern in Functional Programming

Posted on:August 26, 2023 at 01:13 PM

Hey, welcome back! In this lecture, we’re going to dive into the world of the Factory Method pattern in functional programming. The Factory Method pattern is a creational design pattern, which means it focuses on how objects are created. What makes the Factory Method pattern unique is that it allows us to create objects without exposing the actual instantiation logic to the client. In other words, the client remains oblivious to the details of object creation, and the actual type of object being created is kept hidden as well.

A classic example of the Factory Method pattern can be found in the Java API itself. Consider the Calendar class with its getInstance method. When you call this method, it returns an instance of the Calendar class, but you’re unaware of the concrete implementation being used behind the scenes. This encapsulation of object creation is a hallmark of the Factory Method pattern.

Table of contents

Open Table of contents

Sections

Understanding the UML Diagram

Before we delve into the functional implementation, let’s quickly revisit the UML diagram of the Factory Method pattern.

factoryUML

In this diagram, we have the concept of a product, which is a concrete implementation of an interface (or abstract class). The factory class is responsible for creating instances of these products. The factory method is defined within the factory class, and it returns an instance of a specific product type. The client interacts solely with the factory class to obtain instances of products, making the process transparent and isolated from the actual implementation.

Functional Implementation of the Factory Method Pattern

Now that we understand the concept behind the Factory Method pattern, let’s delve into a functional programming example. Imagine we’re tasked with deciding the best type of flooring for different housing units based on the climate or temperature variations in a region. For instance, concrete floors might be a preferred choice in hot climates due to their cooling properties, while wooden or cork floors could be more suitable for other temperature ranges.

Let’s take a look at the code:

interface Flooring {
    void installation();
}

class WoodenFlooring implements Flooring {
    @Override
    public void installation() {
        System.out.println("Wooden flooring installed.");
    }
}

class ConcreteFlooring implements Flooring {
    @Override
    public void installation() {
        System.out.println("Concrete flooring installed.");
    }
}

class CorkFlooring implements Flooring {
    @Override
    public void installation() {
        System.out.println("Cork flooring installed.");
    }
}

class FlooringFactory {
    public static Flooring getFlooring(int minTemperature, int maxTemperature) {
        if (minTemperature <= 5 && maxTemperature <= 20) {
            return new WoodenFlooring();
        } else if (minTemperature <= 5 && maxTemperature >= 45) {
            return new CorkFlooring();
        } else {
            return new ConcreteFlooring();
        }
    }
}

public class FactoryMethodPattern {
    public static void main(String[] args) {
        Flooring floor = FlooringFactory.getFlooring(-1, 18);
        floor.installation();
    }
}

In this example, we have an interface Flooring with concrete implementations WoodenFlooring, ConcreteFlooring, and CorkFlooring. The FlooringFactory class is our factory class that contains the factory method getFlooring. This method takes the minimum and maximum temperature values and returns the appropriate type of flooring based on the temperature range. The FactoryMethodPattern class demonstrates how to use the factory method to obtain and install the right flooring.

Conclusion

Excelent coders! We’ve journeyed through the majestic halls of the factory Design Pattern, likening it to artful navigation through a gallery. We’ve discerned the essence of this pattern – a harmonious symphony between containers and factorys. Our journey through functional implementation has demonstrated the concise and elegant code it offers. So, let’s continue crafting our code with artistic finesse, harmonizing the interactions between objects, and painting beautiful designs in the canvas of Java! 🎨✨

You can find the repo for this section of the course Here