menu
Behavioralarrow_forward_ios
Facade
houseFacade

Description

The Facade Design Pattern is a structural pattern that provides a simplified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use. This pattern is particularly useful when dealing with complex systems or libraries.

Problem

Today’s example will be a program that will start and stop the computer. Our computer will consist of three main classes:
  • CPU
  • Memory
  • Hard Drive
So let’s define these classes and add methods to them that will be used to start our computer correctly.

class CPU
  def process_data
    puts 'CPU: Processing data'
  end
end

class Memory
  def load_data
    puts 'Memory: Loading data'
  end
end

class HardDrive
  def read_data
    puts 'HardDrive: Reading data'
  end
end
To start our computer, we will have to perform three actions defined in the above classes.

# Start Computer
CPU.new.process_data
Memory.new.load_data
HardDrive.new.read_data
This is not too complicated code yet, but we know that in the future we will want to add additional functions, such as stopping or restarting the computer. Anticipating these ideas, we will prepare a class that will be our facade and add a function to it that will allow us to start the computer.

class ComputerFacade
  def initialize
    @cpu = CPU.new
    @memory = Memory.new
    @hard_drive = HardDrive.new
  end

  def start
    puts 'Starting the computer'
    @cpu.process_data
    @memory.load_data
    @hard_drive.read_data
    puts 'Computer started successfully'
  end
end
Now starting the computer becomes much easier because we do not need to know the implementation of each class.

computer = ComputerFacade.new
computer.start
We have already prepared our facade, but to visualize it better, we will add new features to it. First, we will add new functions to the previously defined classes that allow us to stop the computer.

class CPU
  # Rest of the code

  def close_processes
    puts 'CPU: Close all processes'
  end
end

class Memory
  # Rest of the code

  def save_temp_data
    puts 'Memory: Save unsaved data'
  end
end

class HardDrive
  # Rest of the code

  def sleep
    puts 'HardDrive: Time to sleep'
  end
end
Now let’s define a new method that will allow us to stop our computer and also restart it.

class ComputerFacade
  # Rest of the code

  def stop
    puts 'Stop the computer'
    @cpu.close_processes
    @memory.save_temp_data
    @hard_drive.sleep
    puts 'Computer stopped successfully'
  end

  def restart
    stop
    start
  end
end
Now we can easily use our facade to start and stop our computer without knowing the specific implementation of the CPU, Memory and HardDrive classes.

computer = ComputerFacade.new

computer.start
computer.restart
computer.stop
The facade is one of the easier design patterns to understand and implement. Its main advantage is that it simplifies the complexity of the system, but remember not to add too many methods to it to avoid the case where the facade becomes a class that is responsible for everything.