Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Facade appearance pattern

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to achieve the Facade appearance pattern", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to achieve the Facade appearance pattern" can help you solve your doubts.

Facade (appearance mode)

Facade (appearance pattern) belongs to structural pattern, which is a kind of design pattern that is often used in daily development.

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

As an example

If you do not understand the above intention introduction, it does not matter, the design pattern needs to be used in daily work, combined with examples can deepen your understanding, below I have prepared three examples to let you experience in what scenarios this design pattern will be used.

Intention interpretation

Librarian

The library is a very complex system, although books are arranged according to certain rules, but only insiders are relatively clear. As a first-time visitor, if you want to find a book quickly, the best way is to ask the librarian directly. Rather than first understanding the design of the library, because you may have to run back and forth between buildings, the process of borrowing books may also be longer.

The librarian plays a role in simplifying the complexity of the library subsystem. We only need to ask the librarian about everything, without caring about how he deals with the internal system of the library.

One trip at most to provide convenient service

The service of up to one run launched in Zhejiang Province is very convenient, and many business processes have been simplified. Whether it is certificate processing or business acceptance, almost only one run is needed, and the process that must last for a few days will also complete the follow-up process through SMS or App operation.

This is equivalent to the appearance model, because the process within the government system may not change much, but by abstracting the Facade (appearance), ordinary citizens can directly connect with the convenience office, without having to run back and forth between the DMV and the driving school, there is no less behind, but the convenience office does it for you.

Iphone shortcut instruction function

Mobile phones have a lot of App, and we need to know which App each function is on in order to use it freely, while the quick command function can extract some functions of App separately to form a new set of functions. We can only come into contact with "taking photos", "payment" and "calculation", regardless of whether Alipay or Wechat, built-in camera or other camera App is called behind it. There is no need to care about the entrance to the internal functions of the App, these docking is done automatically in the quick connect instruction.

Shortcut instructions are also a mode of appearance.

Intention interpretation

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

In order to reduce the internal complexity of a subsystem with multiple interfaces, we need an appearance to shield the internal complexity, so the appearance pattern is to define a high-level interface, which is directly connected to the internal implementation of the subsystem. However, people who call this high-level interface do not need to care about the internal implementation of the subsystem, which improves ease of use for those who do not want to know about the internal implementation of the subsystem.

Of course, if you want to be deeply customized, you can bypass the appearance pattern and directly use the classes provided by the subsystem, so it is not necessary to call through the appearance pattern, but to determine which invocation method to use according to the actual needs.

Structure diagram

As you can see, Facade points directly to the class in the subsystem, while the class in the subsystem does not point backwards to Facade.

Code example

The following example is written in typescript.

/ / assume that a subsystem is a combination of three classes and is decoupled for abstraction

Class A {

Constructor (b: B) {

This.b = b

}

}

Class B {

Constructor (c: C) {

This.c = c

}

}

Class C {

}

/ / they are combined into a common function, and we can use the details of the appearance pattern masking subclass directly.

Class Compile {

Public run () {

Const parser = new A (new B (new C))

Parser.run ()

}

}

Const compile = new Compile ()

Compile.run ()

So we just need to know the Compile class, not the A B C behind it and its composition.

Malpractice

Appearance mode is not suitable for all scenarios, and it is superfluous to use appearance mode when the subsystem is easy to use.

In addition, when the system is difficult to abstract general functions, the design of appearance patterns may be at a loss, because the scope of application of the designed high-level interface may be very narrow, and the significance of appearance patterns is relatively small.

Read here, this article "how to achieve the Facade appearance pattern" article has been introduced, want to master the knowledge of this article also need everyone to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report