Design Patterns-understanding of appearance patterns

Source: Internet
Author: User

Design Patterns-understanding of appearance patterns

Appearance mode: provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, making the subsystem easier to use.

In the data room charging system, the appearance mode is used to remove the coupling between the U layer and the B layer. According to the previous practice, when the functions in the U layer call the methods in the B layer, you need the U layer to fully understand the methods in the B layer, and the U layer to call the methods in the B layer. This method completely exposes things on the B layer to the U layer, and increases the coupling between the U layer and the B layer, the modification made by layer B should take into account the calling PROBLEM OF THE U layer, which is not conducive to the security of the system. The added appearance mode puts all the components in layer B into the appearance class Fa? When the U layer needs to call the method of layer B? Instead of fully understanding the methods in layer B. This defines a high-level interface between the U layer and B layer, providing a consistent interface for a group of methods in the B layer, this makes the method of layer B more secure and easy to use.

In general, the appearance mode is classification and nesting, and the methods in layer B are classified into Fa? In the ade class, Fa? The ade class does not need to define its own methods. They are nested methods in layer B to implement the functions of high-level interfaces. When the U layer needs to be used, you can go to Fa? Find all the method names in the ade class. You do not need to know who provided these methods, or how they are implemented. Use Fa directly? The method name in the ade class can implement its own functions, which greatly removes the coupling between the U layer and other layers, so that the U layer can concentrate on what the U layer should do, it can also improve system performance. These benefits are also the advantages of the design model.

Example in the book: stock trading and funds. When there is no design pattern, the implementation process is as follows:

 

Class Program {static void Main (string [] args) {Stock1 stock1 = new Stock1 (); Stock2 stock2 = new Stock2 (); stock1.Buy (); stock2.Buy (); stock1.Sell (); stock2.Sell (); Console. read () ;}} class Stock1 {public void resume () {Console. writeLine ("stock 1 sold");} public void Buy () {Console. writeLine ("stock 1 buy") ;}} class Stock2 {public void detail () {Console. writeLine ("stock 2 sold");} public void Buy () {Console. writeLine ("stock 2 buy ");}}
The result is as follows:

 


The code after the appearance mode is used is as follows:

 

Class Program {static void Main (string [] args) {Facade facade = new Facade (); facade. buyFacade (); facade. sellFacade (); Console. read () ;}} class Facade {Stock1 stock1; Stock2 stock2; public Facade () {stock1 = new Stock1 (); stock2 = new Stock2 ();} public void BuyFacade () {stock1.Buy (); stock2.Buy ();} public void SellFacade () {stock1.Sell (); stock2.Sell () ;}} class Stock1 {public void detail () {Console. writeLine ("stock 1 sold");} public void Buy () {Console. writeLine ("stock 1 buy") ;}} class Stock2 {public void detail () {Console. writeLine ("stock 2 sold");} public void Buy () {Console. writeLine ("stock 2 buy ");}}
The results are the same as above. This example is very concise, so I just want to explain the truth. Please forgive me for any mistakes. When executing methods in the Facade class of the appearance class, you can remove the SellFacade () method if you do not want to sell them in the previous example. The application method is flexible and changeable, your perception is very important.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.