Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

elixir

What is a factory pattern in Elixir?

Muaz Ali

Overview

Design patterns are a set of general approaches in object-oriented programming that are meant to resolve certain types of commonly experienced problems. Factory patterns fall under the category of creational design patterns.

Factory patterns allow end-users to construct objects without revealing the underlying methods in the parent interface. They are a powerful design pattern because they streamline the process of creating new objects without users having to deal with a complex creation process. Let's see how they're implemented.

Code

defmodule Building do
  def info({:room, beds, equipment}),
    do: IO.puts("The room has #{beds} beds and #{equipment}.")
  def info({:store, storage_capacity}),
    do: IO.puts("The store has #{storage_capacity}kg capacity.")
end
Create a building named module

First, we build a module with the defmodule keyword, followed by the Building module, and a module’s body. Here, we’ll explore the module’s functions. In this example, we define two info functions with the same name but different parameters and bodies. The first function of this module displays the number of rooms and equipment passed in the arguments using IO to control the function’s input/output activities. The other function has a similar parameter which shows the storeroom and capacity detail.

Let’s move on to the next module.

defmodule BuildingFactory do
  def create(:store, %{capacity: capacity}), do: {:store, capacity}
  def create(:room, %{beds: beds, chairs: chairs}), do: {:room, beds, "#{chairs} chairs"}
  def create(:room, %{beds: beds, tables: tables}), do: {:room, beds, "#{tables} tables"}
end
Create a BuildingFactory module

Here, we create a second module named BuildingFactory using the defmodule keyword. We use three functions with the same name, create, which have different parameters. For example, in our first function, we use the variable, :store, as the parameter. After the :store variable, we use the map data structure by entering %{capacity: capacity} in the parameters. In the end, we use a tuple, {:store, capacity}, that serves as a container for grouping data. All other functions in this module use a similar function structure for room data in the module.

Let’s look at the program for a better understanding of how we use these modules.

Example

main.exs
building_factory.ex
defmodule Main do

    def main do
        store = BuildingFactory.create(:store, %{capacity: 4})
        Building.info(store)

        room = BuildingFactory.create(:room, %{beds: 2, chairs: 3})
        Building.info(room)

        room = BuildingFactory.create(:room, %{beds: 3, tables: 6})
        Building.info(room)
    end
end
A factory pattern that builds different types of buildings

Note: We need to insert Code.require_file("building_factory.ex") after line 1 and Main.main at the end of main.exs file to run this code locally.

Explanation

  • Line 1: We create a module, Main.
  • Line 3: We call the main function.
  • Line 4: We use the :store variable as a parameter and call the BuildingFactory function from building_factory.ex.
  • Line 5: We call the Building functions and pass values to their parameters accordingly.
  • Line 7: We call the BuildingFactory function with the room variable from building_factory.ex.
  • Line 8: We call the Building functions again and pass values to their parameters accordingly.

RELATED TAGS

elixir
RELATED COURSES

View all Courses

Keep Exploring