Skip to content

Latest commit

 

History

History
260 lines (203 loc) · 5.21 KB

Facade.md

File metadata and controls

260 lines (203 loc) · 5.21 KB

Facade Pattern

The facade pattern is a structural design pattern that provides a simplified interface to a complex subsystem. It hides the complexities of the system and provides a single point of access to its functionalities. By offering a high-level interface, the facade pattern makes it easier for clients to interact with the system, reducing the learning curve and improving usability. This pattern is particularly useful when dealing with complex systems or libraries, as it promotes loose coupling between the client and the subsystem, enhancing maintainability and scalability.

Go Example

package main

import "fmt"

type CPU struct{}

func (c *CPU) Freeze() {
    fmt.Println("Freezing processor...")
}

func (c *CPU) Jump(position int) {
    fmt.Printf("Jumping to position %d...\n", position)
}

func (c *CPU) Execute() {
    fmt.Println("Executing instructions...")
}

type Memory struct{}

func (m *Memory) Load(position int, data string) {
    fmt.Printf("Loading %s into position %d...\n", data, position)
}

type HardDrive struct{}

func (hd *HardDrive) Read(lba, size int) string {
    return fmt.Sprintf("Read %d bytes from LBA %d.", size, lba)
}

type ComputerFacade struct {
    cpu       *CPU
    memory    *Memory
    hardDrive *HardDrive
}

func NewComputerFacade() *ComputerFacade {
    return &ComputerFacade{
        cpu:       &CPU{},
        memory:    &Memory{},
        hardDrive: &HardDrive{},
    }
}

func (cf *ComputerFacade) Start() {
    cf.cpu.Freeze()
    cf.memory.Load(0, cf.hardDrive.Read(100, 1024))
    cf.cpu.Jump(0)
    cf.cpu.Execute()
}

func main() {
    computer := NewComputerFacade()
    computer.Start()
}

Perl Example

package CPU;
sub new { bless {}, shift }
sub freeze { print "Freezing processor...\n"; }
sub jump { my ($self, $position) = @_; print "Jumping to position $position...\n"; }
sub execute { print "Executing instructions...\n"; }

package Memory;
sub new { bless {}, shift }
sub load { my ($self, $position, $data) = @_; print "Loading $data into position $position...\n"; }

package HardDrive;
sub new { bless {}, shift }
sub read { my ($self, $lba, $size) = @_; return "Read $size bytes from LBA $lba."; }

package ComputerFacade;
sub new {
    my $class = shift;
    bless {
        cpu => CPU->new,
        memory => Memory->new,
        hard_drive => HardDrive->new
    }, $class;
}
sub start {
    my $self = shift;
    $self->{cpu}->freeze;
    $self->{memory}->load(0, $self->{hard_drive}->read(100, 1024));
    $self->{cpu}->jump(0);
    $self->{cpu}->execute;
}

# Client code
my $computer = ComputerFacade->new;
$computer->start;

Python Example

class CPU:
    def freeze(self):
        print("Freezing processor...")

    def jump(self, position):
        print(f"Jumping to position {position}...")

    def execute(self):
        print("Executing instructions...")


class Memory:
    def load(self, position, data):
        print(f"Loading {data} into position {position}...")


class HardDrive:
    def read(self, lba, size):
        return f"Read {size} bytes from LBA {lba}."


class ComputerFacade:
    def __init__(self):
        self.cpu = CPU()
        self.memory = Memory()
        self.hard_drive = HardDrive()

    def start(self):
        self.cpu.freeze()
        self.memory.load(0, self.hard_drive.read(100, 1024))
        self.cpu.jump(0)
        self.cpu.execute()


# Client code
computer = ComputerFacade()
computer.start()

Ruby Example

class CPU
  def freeze
    puts "Freezing processor..."
  end

  def jump(position)
    puts "Jumping to position #{position}..."
  end

  def execute
    puts "Executing instructions..."
  end
end

class Memory
  def load(position, data)
    puts "Loading #{data} into position #{position}..."
  end
end

class HardDrive
  def read(lba, size)
    "Read #{size} bytes from LBA #{lba}."
  end
end

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

  def start
    @cpu.freeze
    @memory.load(0, @hard_drive.read(100, 1024))
    @cpu.jump(0)
    @cpu.execute
  end
end

# Client code
computer = ComputerFacade.new
computer.start

Rust Example

struct CPU;

impl CPU {
    fn freeze(&self) {
        println!("Freezing processor...");
    }

    fn jump(&self, position: u32) {
        println!("Jumping to position {}...", position);
    }

    fn execute(&self) {
        println!("Executing instructions...");
    }
}

struct Memory;

impl Memory {
    fn load(&self, position: u32, data: &str) {
        println!("Loading {} into position {}...", data, position);
    }
}

struct HardDrive;

impl HardDrive {
    fn read(&self, lba: u32, size: u32) -> String {
        format!("Read {} bytes from LBA {}.", size, lba)
    }
}

struct ComputerFacade {
    cpu: CPU,
    memory: Memory,
    hard_drive: HardDrive,
}

impl ComputerFacade {
    fn new() -> Self {
        Self {
            cpu: CPU,
            memory: Memory,
            hard_drive: HardDrive,
        }
    }

    fn start(&self) {
        self.cpu.freeze();
        self.memory.load(0, &self.hard_drive.read(100, 1024));
        self.cpu.jump(0);
        self.cpu.execute();
    }
}

fn main() {
    let computer = ComputerFacade::new();
    computer.start();
}