Sobes.tech
Junior — Middle

Can you explain what the Facade design pattern is and in what cases it is used?

sobes.tech AI

Answer from AI

The Facade design pattern provides a simple interface to a complex system of classes, library, or framework. Its main goal is to hide the complexity of the subsystem and provide the client with a convenient and understandable interface.

Used when:

  • You need to simplify interaction with a large number of classes.
  • You want to reduce dependencies between client code and the complex subsystem.
  • You need to provide a unified interface for multiple subsystem interfaces.

Example in Kotlin for Android:

// Complex subsystem
class AudioManager {
    fun playSound() { /* ... */ }
}

class VideoManager {
    fun playVideo() { /* ... */ }
}

// Facade
class MediaFacade(private val audioManager: AudioManager, private val videoManager: VideoManager) {
    fun playMedia() {
        audioManager.playSound()
        videoManager.playVideo()
    }
}

// Usage
val mediaFacade = MediaFacade(AudioManager(), VideoManager())
mediaFacade.playMedia()

Thus, the client does not need to know the details of how audio and video managers work — it interacts only with the facade.