1. Definition
The Facade Design Pattern provides a unified interface to a set of interfaces in a subsystem. It defines a higher-level interface that makes the subsystem easier to use, encapsulating complex functionality behind a simple interface.
2. Problem Statement
Imagine working with a complex library or system having a multitude of interdependent classes. It's cumbersome and intimidating to instantiate and manage all these classes directly from the client code.
3. Solution
The solution is to introduce a Facade class as a single, simplified entry point to the complex system or library. This Facade provides convenient methods for the most common tasks and manages the life cycle of intricate system components.
4. Real-World Use Cases
1. A computer starting up involves multiple processes like checking hardware, loading system files, and establishing network connections. A facade can provide a simple "Start" method to handle all these.
2. A multimedia player dealing with various file formats, codecs, and network streaming can have a facade with simple play, pause, and stop methods.
5. Implementation Steps
1. Identify a simple, common interface that would be appropriate for client use.
2. Design a Facade class that provides methods implementing this interface, and these methods internally call multiple methods on the subsystem.
3. Client uses the Facade instead of calling subsystem classes directly.
6. Implementation in Scala Programming
// Subsystem classes
class CPU {
def start(): Unit = println("CPU started")
def shutDown(): Unit = println("CPU shut down")
}
class Memory {
def load(): Unit = println("Memory loaded")
def clear(): Unit = println("Memory cleared")
}
class Disk {
def read(): Unit = println("Disk read initiated")
def write(): Unit = println("Disk write initiated")
}
// Facade
class Computer {
private val cpu = new CPU
private val memory = new Memory
private val disk = new Disk
def startComputer(): Unit = {
cpu.start()
memory.load()
disk.read()
println("Computer started")
}
def shutDownComputer(): Unit = {
disk.write()
memory.clear()
cpu.shutDown()
println("Computer shut down")
}
}
// Client
object Main extends App {
val computer = new Computer
computer.startComputer()
computer.shutDownComputer()
}
Output:
CPU started Memory loaded Disk read initiated Computer started Disk write initiated Memory cleared CPU shut down Computer shut down
Explanation:
1. We have a subsystem with classes CPU, Memory, and Disk. Each class has its methods and operations.
2. Instead of managing these classes directly from the client, we introduce a Facade, the Computer class.
3. The Computer class has simplified methods like startComputer and shutDownComputer which internally manage the subsystem's components.
4. The client then simply interacts with the Computer facade, making the process more intuitive and hiding the complexity of the subsystem.
7. When to use?
Use the Facade pattern when:
1. You want to provide a simpler interface to a complex subsystem.
2. There are multiple dependencies or interactions in the subsystem, and you want to shield clients from this complexity.
3. You want to layer your subsystems and expose only what's necessary through facades.
Comments
Post a Comment