The schema definition

Providing a consistent (stable) interface for a set of interfaces in a subsystem, the Facade pattern defines a high-level interface that makes the subsystem easier to use (reuse)

The class diagram

Application scenarios

1. Use the Facade pattern when you need to use a limited but direct interface to a complex subsystem

2. When you need to organize subsystems into layers, use the Facade pattern

advantages

Simplify client invocation

The point to summarize

The point to summarize

  • From the client’s point of view, the Facade pattern simplifies the interface of the entire component system, achieving a “decoupling” effect between the component’s internal and external clients — any change in the internal subsystem does not affect the change in the Facade interface
  • The Facade design pattern focuses more on looking at the entire system at the architectural level rather than at the level of individual classes, and a Facade is often an architectural design pattern
  • The Facade design pattern is not a container that can be arbitrarily placed into any number of objects. The components in the Facade pattern should be “a series of components coupled to each other” rather than a simple set of functions

Go language code implementation

Project directory

facade.go

package Facade

import "fmt"

type CarModel struct {}

func NewCarModel() *CarModel {
   return &CarModel{}
}

func (c * CarModel) SetModel () {
   fmt.Println("CarModel - SetModel")
}

type CarEngine struct {}
func NewCarEngine () *CarEngine {
   return &CarEngine{}
}

func (c *CarEngine) SetEngine (){
   fmt.Println("CarEngine - SetEngine")
}

type CarBody struct {}
func NewCarBody () *CarBody {
   return &CarBody{}
}

func (c *CarBody) SetCarBody() {
   fmt.Println("CarBody - SetBody")
}

type CarFacade struct {
   model CarModel
   engine CarEngine
   body CarBody
}

func NewCarFacade() * CarFacade{
   return &CarFacade{
      model:  CarModel{},
      engine: CarEngine{},
      body:   CarBody{},
   }
}

func (c *CarFacade) CreateCompleteCar() {
   c.model.SetModel()
   c.engine.SetEngine()
   c.body.SetCarBody()
}

facade_test.go

package Facade

import "testing"

func TestCarFacade_CreateCompleteCar(t *testing.T) {
   facade := CarFacade{}
   facade.CreateCompleteCar()
}