SoFunction
Updated on 2025-03-01

A brief analysis of the Facade (appearance) mode of Go design mode

In our daily development, we often face complex subsystems, which contain many interrelated classes and interfaces. Direct use of these classes and interfaces may result in increased code complexity, making it difficult for the system to maintain and understand. To solve this problem, we can use the appearance pattern to provide a simplified interface that hides the complexity of the underlying subsystem, so that clients can use subsystem functions more conveniently.

What is appearance mode?

Facade Pattern is a structural design pattern that encapsulates one or more complex subsystems by providing a unified interface to provide a simplified access method for clients. The appearance pattern decouples the interface and implementation details of complex subsystems from client code, so that the client only needs to interact with the appearance objects without understanding the complexity of the underlying subsystem.

Structure of appearance pattern

Appearance mode contains the following characters:

  • Facade: The appearance object is the intermediate layer between the client and the subsystem. It provides a simplified interface to delegate client requests to the subsystem for processing. The appearance object knows which subsystem classes are responsible for handling requests and dispatches requests to them.
  • Subsystem: A subsystem is a set of classes or interfaces that implement specific functions. The appearance object forwards the client's request to the appropriate subsystem class for processing.
  • Client: The client accesses the functions of the subsystem through appearance objects. It only needs to interact with appearance objects, without directly dealing with subsystem classes.

How it works

  • The client accesses the functionality of the subsystem by instantiating the appearance object.
  • The appearance object receives the client's request and delegates the request to the appropriate subsystem class for processing based on the request type and parameters.
  • After the subsystem class receives the request, it executes the corresponding functional logic.
  • The appearance object returns the subsystem's processing results to the client.

Code implementation

In Golang, the appearance pattern can be implemented by defining an appearance structure. The appearance structure encapsulates the complexity of the underlying subsystem and provides a simplified approach for client use. Here is an example of a simple Golang appearance pattern:

func NewAPI() API {
	return &apiImpl{
		a: NewAModuleAPI(),
		b: NewBModuleAPI(),
	}
}

// API is facade interface of facade package
type API interface {
	Test() string
}

// apiImpl facade implement
type apiImpl struct {
	a AModuleAPI
	b BModuleAPI
}

func (a *apiImpl) Test() string {
	aRet := ()
	bRet := ()
	return ("\n%s\n%s\n", aRet, bRet)
}

// NewAModuleAPI return new AModuleAPI
func NewAModuleAPI() AModuleAPI {
	return &aModuleImpl{}
}

// AModuleAPI ...
type AModuleAPI interface {
	TestA() string
}

type aModuleImpl struct{}

func (*aModuleImpl) TestA() string {
	return "A module running"
}

// NewBModuleAPI return new BModuleAPI
func NewBModuleAPI() BModuleAPI {
	return &bModuleImpl{}
}

// BModuleAPI ...
type BModuleAPI interface {
	TestB() string
}

type bModuleImpl struct{}

func (*bModuleImpl) TestB() string {
	return "B module running"
}


func TestFacadeAPI(t *) {
	api := NewAPI()
	ret := ()
	(ret)
}

/**Output
 A module running
 B module running
 **/

It can be seen that the client only needs to interact with the appearance object and complete the corresponding operations by calling the appearance object method without understanding the complexity of the underlying subsystem.

Appearance patterns can help us simplify the use of complex subsystems in actual development and provide consistent interfaces for clients to use. It can reduce the coupling of code and improve the maintainability and readability of code.

Summarize:

This article introduces the concept, structure, and working principles of appearance patterns and provides a simple example of Golang implementation. In actual development, appearance patterns can help us simplify the use of complex subsystems and provide consistent interfaces for client calls. By using appearance mode, the coupling of the code can be reduced and the maintainability and readability of the code can be improved.

The above is a detailed analysis of the Facade (appearance) mode of Go design mode. For more information about the Go Facade mode, please follow my other related articles!