Facade Pattern is also called Facade Design Pattern. The facade pattern provides a unified set of interfaces for the subsystem and defines a set of high-level interfaces to make the subsystem easier to use. The idea of facade patterns is more commonly used in architectural design, and rarely mentioned in code, but quietly used all the time.

UML class diagrams location: www.processon.com/diagraming/…

This article is linked to: github.com/shidawuhen/…

1 definition

1.1 Facade mode

Facade pattern: Provides a consistent interface for a set of interfaces in a subsystem. This pattern defines a high-level interface that makes the subsystem easier to use.

UML:

1.2 analysis

There is not much good analysis of the facade mode, the idea is relatively simple. There are many subsystems in our system. To complete a task, multiple subsystems need to cooperate.

We can choose to expose all interfaces of the subsystem to the Client and let the Client call them. However, this will lead to some problems. First, communication costs will be very high in the later stage. Multiple interfaces need to be called to complete a function. Second, if there are multiple clients, the same code Client needs to be repeatedly developed, and later code changes, all parties will be very annoying and laborious. Third, the impact on response time and performance, multiple interface round-trip, nothing to increase a lot of communication time and requests.

Another approach is to provide only one interface for a given function wrapped on the system side. The benefits are many, including low communication cost, no repetitive development of the Client, small impact of functional changes, improved response time and performance. Generally, these interfaces have corresponding OpenAPI, realizing the effect of opening functions to the outside world.

2. Application scenarios

For a usage scenario, give a simple example.

The e-commerce system generally includes subsystems such as commodity, inventory, marketing, merchant, transaction, payment, after-sales, contract performance, logistics, storage and so on. Take the product details page for example, the business details page interface will generally involve the product, inventory, marketing, business and other systems. The clients of the e-commerce system include PC, Mobile, Android, IOS, etc. If these clients call interfaces to piece together the data of the business page, it feels that students on the client side can hold a machete and have a heart-to-heart talk with students on the server side. To avoid this, the general commodity group uses the provider detail page interface, which retrieves all information about the detail page and returns it to the client.

Of course, if the traffic is heavy and the interface performance needs to be optimized, you can split the interface according to the actual situation. The client needs to request multiple interfaces, but even so, the related interfaces are encapsulated. If you encounter such a split in the real world, congratulations, it shows that the company is growing, traffic is increasing, can promote the growth of everyone faster.

3. Code implementation

The facade pattern should be the most natural design pattern to use. Briefly write the e-business system facade mode code, so as to be consistent with other articles.

package main

import "fmt"

type ProductSystem struct{}func (p *ProductSystem) GetProductInfo(a) {
   fmt.Println("Obtain commodity information")}type StockSystem struct{}func (s *StockSystem) GetStockInfo(a) {
   fmt.Println("Get inventory information")}type PromotionSystem struct{}func (p *PromotionSystem) GetPromotionInfo(a) {
   fmt.Println("Access to marketing information")}func ProductDetail(a) {
   product := &ProductSystem{}
   stock := &StockSystem{}
   promotion := &PromotionSystem{}
   product.GetProductInfo()
   stock.GetStockInfo()
   promotion.GetPromotionInfo()
   fmt.Println("Collate all data of product details page")}func main(a) {
   ProductDetail()
}

Copy the code

Output:

➜ myproject go run main.go

Obtain commodity information

Get inventory information

Access to marketing information

Finish all data of product detail page

3. Summary

The facade pattern is one of the most commonly used design patterns by programmers, and it often comes naturally in architectural design. The interface isolation principle and Demeter’s rule are well satisfied.

The last

If you like my article, you can follow my public account (Programmer Malatang)

My personal blog is shidawuhen.github. IO /

Review of previous articles:

recruitment

  1. Bytes to beat | push big 24:00

  2. Bytes to beat | headlines today guangzhou server push r&d engineers

  3. Bytes to beat | trill electricity now hiring front-end development project in Shanghai

  4. Bytes to beat | trill electricity senior server-side development engineer – trading in Shanghai

  5. Bytes to beat | trill electric ShangWuHan server-side development engineer (senior)

  6. Bytes to beat | fly book big customer push product manager

  7. Bytes to beat | trill electricity service side technical posts vacant

  8. Bytedance recruitment special

Design patterns

  1. Go Design Pattern (14)- Adapter pattern

  2. Go Design Mode (13)- Decorator mode

  3. Go Design Mode (12)- Bridge mode

  4. Go Design Pattern (11)- Proxy pattern

  5. Go Design Mode (10)- Prototype mode

  6. Go Design Mode (9)- Builder mode

  7. Go Design Pattern (8)- Abstract Factory

  8. Go Design Mode (7)- Factory Mode

  9. Go Design Pattern (6)- Singleton pattern

  10. Go Design Pattern (5)- Class diagram symbolic representation

  11. Go Design Pattern (4)- Code writing optimization

  12. Go Design Pattern (4)- Code writing

  13. Go Design Patterns (3)- Design principles

  14. Go Design Pattern (2)- Object-oriented analysis and design

  15. Go Design Pattern (1)- Syntax

language

  1. No more fear of not getting Gin request data

  2. Understand pprof

  3. Go tool generate

  4. Go singleton implementation scheme

  5. Implementation principle of Go channel

  6. Implementation principle of Go timer

  7. Beego framework use

  8. Golang source BUG tracking

  9. Gin framework concise version

  10. Gin source code analysis

architecture

  1. The paging check pit is designed

  2. Payment access general issues

  3. Current limiting 2

  4. Seconds kill system

  5. Distributed systems and consistency protocols

  6. Service framework and registry for microservices

  7. Discussion on Micro-service

  8. Current limiting implementation 1

  9. CDN request process details

  10. Common Cache tips

  11. How to effectively connect with third-party payment

  12. Algorithm is summarized

storage

  1. MySQL development specification

  2. Redis implements distributed locking

  3. The implementation principle of atomicity, consistency and persistence of transactions

  4. InnoDB locks and transactions

network

  1. HTTP2.0 basics tutorial

  2. HTTPS Configuration Combat

  3. HTTPS Connection Process

  4. TCP Performance Optimization

tool

  1. GoLand Practical skills

  2. Automatically generate go struct from mysql table

  3. Markdown editor recommends – Typora

Reading notes

  1. Selected by MAO

  2. The principle of

  3. History As A Mirror

  4. Agile revolution

  5. How to exercise your memory

  6. Simple Logic – After reading

  7. Hot Wind – After reading

  8. Analects of Confucius – After reading

  9. Sun Tzu’s Art of War – Reflections from reading

thinking

  1. The experience of calling 119 at night

  2. Struggle to mobilize all forces for victory

  3. Anti-liberalism

  4. practical

  5. The standard by which you judge yourself

  6. Service team holiday shift plan

  7. Project process management

  8. Some thoughts on project management

  9. Some thoughts on product manager

  10. Thinking about programmer career development

  11. Thinking about code review