Facade pattern

The facade pattern (also spelled façade) is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex underlying or structural code. A facade can:

  • improve the readability and usability of a software library by masking interaction with more complex components behind a single (and often simplified) API
  • provide a context-specific interface to more generic functionality (complete with context-specific input validation)
  • serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favor of more loosely-coupled code

Developers often use the facade design pattern when a system is very complex or difficult to understand because the system has many interdependent classes or because its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class that contains a set of members required by the client. These members access the system on behalf of the facade client and hide the implementation details.

Overview

The Facade [1] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.

What problems can the Facade design pattern solve? [2]

  • To make a complex subsystem easier to use, a simple interface should be provided for a set of interfaces in the subsystem.
  • The dependencies on a subsystem should be minimized.

Clients that access a complex subsystem directly refer to (depend on) many different objects having different interfaces (tight coupling), which makes the clients hard to implement, change, test, and reuse.

What solution does the Facade design pattern describe?

Define a Facade object that

  • implements a simple interface in terms of (by delegating to) the interfaces in the subsystem and
  • may perform additional functionality before/after forwarding a request.

This enables to work through a Facade object to minimize the dependencies on a subsystem.
See also the UML class and sequence diagram below.

Usage

A Facade is used when an easier or simpler interface to an underlying object is desired.[3] Alternatively, an adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior. A decorator makes it possible to add or alter behavior of an interface at run-time.

PatternIntent
AdapterConverts one interface to another so that it matches what the client is expecting
DecoratorDynamically adds responsibility to the interface by wrapping the original code
FacadeProvides a simplified interface

The facade pattern is typically used when

  • a simple interface is required to access a complex system,
  • a system is very complex or difficult to understand,
  • an entry point is needed to each level of layered software, or
  • the abstractions and implementations of a subsystem are tightly coupled.

Structure

UML class and sequence diagram

A sample UML class and sequence diagram for the Facade design pattern.

In this UML class diagram, the Client class doesn't access the subsystem classes directly. Instead, the Client works through a Facade class that implements a simple interface in terms of (by delegating to) the subsystem classes (Class1, Class2, and Class3). The Client depends only on the simple Facade interface and is independent of the complex subsystem.[4]

The sequence diagram shows the run-time interactions: The Client object works through a Facade object that delegates the request to the Class1, Class2, and Class3 instances that perform the request.

UML class diagram

Facade
The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
Clients
The objects are using the Facade Pattern to access resources from the Packages.

Example

This is an abstract example of how a client ("you") interacts with a facade (the "computer") to a complex system (internal computer parts, like CPU and HardDrive).

C++

#include <memory>

class CPU {
 public:
  void Freeze();
  void Jump(long position);
  void Execute();
};

class HardDrive {
 public:
  char* Read(long lba, int size);
};

class Memory {
 public:
  void Load(long position, char* data);
};

// Facade:
class ComputerFacade {
 public:
  void Start() {
    cpu_->Freeze();
    memory_->Load(kBootAddress, hard_drive_->Read(kBootSector, kSectorSize));
    cpu_->Jump(kBootAddress);
    cpu_->Execute();
  }

 private:
  std::unique_ptr<CPU> cpu_ = std::make_unique<CPU>();
  std::unique_ptr<Memory> memory_ = std::make_unique<Memory>();
  std::unique_ptr<HardDrive> hard_drive_ = std::make_unique<HardDrive>();
};

int main() {
  ComputerFacade computer;
  computer.Start();
}

C#

Implementation

namespace DesignPattern.Facade
{
    class SubsystemA
    {
        public string OperationA1()
        {
            return "Subsystem A, Method A1\n";
        }
        public string OperationA2()
        {
            return "Subsystem A, Method A2\n";
        }
    }

    class SubsystemB
    {
        public string OperationB1()
        {
            return "Subsystem B, Method B1\n";
        }

        public string OperationB2()
        {
            return "Subsystem B, Method B2\n";
        }
    }

    class SubsystemC
    {
        public string OperationC1()
        {
            return "Subsystem C, Method C1\n";
        }

        public string OperationC2()
        {
            return "Subsystem C, Method C2\n";
        }
    }

    public class Facade
    {
        private readonly SubsystemA a = new SubsystemA();
        private readonly SubsystemB b = new SubsystemB();
        private readonly SubsystemC c = new SubsystemC();
        public void Operation1()
        {
            Console.WriteLine("Operation 1\n" +
                a.OperationA1() +
                b.OperationB1() +
                c.OperationC1());
        }
        public void Operation2()
        {
            Console.WriteLine("Operation 2\n" +
                a.OperationA2() +
                b.OperationB2() +
                c.OperationC2());
        }
    }
}

Sample code

namespace DesignPattern.Facade.Sample
{
    // The 'Subsystem ClassA' class
    class CarModel
    {
        public void SetModel()
        {
            Console.WriteLine(" CarModel - SetModel");
        }
    }

    /// <summary>
    /// The 'Subsystem ClassB' class
    /// </summary>
    class CarEngine
    {
        public void SetEngine()
        {
            Console.WriteLine(" CarEngine - SetEngine");
        }
    }

    // The 'Subsystem ClassC' class
    class CarBody
    {
        public void SetBody()
        {
            Console.WriteLine(" CarBody - SetBody");
        }
    }

    // The 'Subsystem ClassD' class
    class CarAccessories
    {
        public void SetAccessories()
        {
            Console.WriteLine(" CarAccessories - SetAccessories");
        }
    }

    // The 'Facade' class
    public class CarFacade
    {
        private readonly CarAccessories accessories;
        private readonly CarBody body;
        private readonly CarEngine engine;
        private readonly CarModel model;

        public CarFacade()
        {
            accessories = new CarAccessories();
            body = new CarBody();
            engine = new CarEngine();
            model = new CarModel();
        }

        public void CreateCompleteCar()
        {
            Console.WriteLine("******** Creating a Car **********");
            model.SetModel();
            engine.SetEngine();
            body.SetBody();
            accessories.SetAccessories();

            Console.WriteLine("******** Car creation is completed. **********");
        }
    }

    // Facade pattern demo
    class Program
    {
        static void Main(string[] args)
        {
            var facade = new CarFacade();

            facade.CreateCompleteCar();

            Console.ReadKey();
        }
    }
}

Java

/* Complex parts */

class CPU {
    public void freeze() { ... }
    public void jump(long position) { ... }
    public void execute() { ... }
}

class HardDrive {
    public byte[] read(long lba, int size) { ... }
}

class Memory {
    public void load(long position, byte[] data) { ... }
}

/* Facade */

class ComputerFacade {
    private final CPU processor;
    private final Memory ram;
    private final HardDrive hd;

    public ComputerFacade() {
        this.processor = new CPU();
        this.ram = new Memory();
        this.hd = new HardDrive();
    }

    public void start() {
        processor.freeze();
        ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
        processor.jump(BOOT_ADDRESS);
        processor.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) {
        ComputerFacade computer = new ComputerFacade();
        computer.start();
    }
}

Ruby

# Complex Parts
class CPU
  def freeze; end
  def jump(position); end
  def execute; end
end

class Memory
  def load(position, data); end
end

class HardDrive
  def read(lba, size); end
end

# Facade
class ComputerFacade

  def initialize
    @processor = CPU.new
    @ram = Memory.new
    @hd = HardDrive.new
  end

  def start
    @processor.freeze
    @ram.load(BOOT_ADDRESS, @hd.read(BOOT_SECTOR, SECTOR_SIZE))
    @processor.jump(BOOT_ADDRESS)
    @processor.execute
  end
end

# Client
computer_facade = ComputerFacade.new
computer_facade.start

PHP

/**
 * The complicated, underlying logic.
 */
class CPU
{
    public function freeze() {/* ... */}
    public function jump($position) {/* ... */}
    public function execute() {/* ... */}
}

class Memory
{
    public function load($position, $data) {/* ... */}
}

class HardDrive
{
    public function read($lba, $size) {/* ... */}
}

/**
 * The facade that users would be interacting with.
 */
class ComputerFacade
{
    protected CPU $cpu;
    protected Memory $memory;
    protected HardDrive $hd;

    public function __construct()
    {
        $this->cpu = new CPU;
        $this->memory = new Memory;
        $this->hd = new HardDrive;
    }

    public function start() : void
    {
        $this->cpu->freeze();
        $this->memory->load(BOOT_ADDRESS, $this->hd->read(BOOT_SECTOR, SECTOR_SIZE));
        $this->cpu->jump(BOOT_ADDRESS);
        $this->cpu->execute();
    }
}

/**
 * How a user could start the computer.
 */
$computer = new ComputerFacade;
$computer->start();

Python

"""
Facade pattern example.
"""

# Complex computer parts
class CPU(object):
    """
    Simple CPU representation.
    """
    def freeze(self) -> None:
        print("Freezing processor.")

    def jump(self, position) -> None:
        print("Jumping to:", position)

    def execute(self) -> None:
        print("Executing.")


class Memory(object):
    """
    Simple memory representation.
    """
    def load(self, position, data):
        print("Loading from {0} data: '{1}'.".format(position, data))


class SolidStateDrive(object):
    """
    Simple solid state drive representation.
    """
    def read(self, lba, size):
        return "Some data from sector {0} with size {1}".format(lba, size)


class ComputerFacade(object):
    """
    Represents a facade for various computer parts.
    """
    def __init__(self) -> None:
        self.cpu = CPU()
        self.memory = Memory()
        self.ssd = SolidStateDrive()

    def start(self) -> None:
        self.cpu.freeze()
        self.memory.load("0x00", self.ssd.read("100", "1024"))
        self.cpu.jump("0x00")
        self.cpu.execute()


computer_facade = ComputerFacade()
computer_facade.start()

Output:

Freezing processor.
Loading from 0x00 data: 'Some data from sector 100 with size 1024'.
Jumping to: 0x00
Executing.


PowerShell

"""
Facade pattern example.
"""

class CPU {
    
    freeze() {
        "$this freezing" | Out-Host
    }

    jump($position) {
        "$this jump to $position" | Out-Host
    }

    execute() {
        "$this execute" | Out-Host
    }
}

class Memory {
    load($position, $data) {
        "$this load $position $data" | Out-Host        
    }
}

class HardDrive {
    [object] read($lba, $size) {        
        "$this read $lba $size" | Out-Host        
        
        return "(some data)"
    }
}

# Façade
class Computer {
    hidden [CPU]$cpu 
    hidden [Memory]$memory
    hidden [HardDrive]$hardDrive

    Computer() {
        $this.cpu = New-Object CPU
        $this.memory = New-Object Memory
        $this.hardDrive = New-Object HardDrive
    }

    StartComputer() {
        $this.cpu.freeze()
        $this.memory.load(0, $this.hardDrive.read(0, 1024))
        $this.cpu.jump(10)
        $this.cpu.execute()
    }
}


$façade = [Computer]::new()

$façade.StartComputer()

Output:

CPU freezing
HardDrive read 0 1024
Memory load 0 (some data)
CPU jump to 10
CPU execute

See also

References

  1. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 185ff. ISBN 0-201-63361-2.CS1 maint: multiple names: authors list (link)
  2. "The Facade design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-12.
  3. Freeman, Eric; Freeman, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Hendrickson, Mike; Loukides, Mike (eds.). Head First Design Patterns (paperback). 1. O'Reilly. pp. 243, 252, 258, 260. ISBN 978-0-596-00712-6. Retrieved 2012-07-02.
  4. "The Facade design pattern - Structure and Collaboration". w3sDesign.com. Retrieved 2017-08-12.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.