(17) Façade mode

Source: Internet
Author: User

Tags: java design mode façade mode

(+) façade mode

definition: also called the appearance pattern, requires that the outside of a subsystem and its internal communication must be carried out through a unified object. Façade mode provides a high-level interface that makes subsystems easier to use.

type: structural mode

Class Diagram:

Façade mode structure:

  • facade façade role: the client can invoke the method of this role. This role knows all the functions and responsibilities of the subsystem. In general, this role delegates all requests from the client to the appropriate subsystem, saying that the role has no actual business logic, just a delegate class.

  • Subsystem subsystem role: You can have one or more subsystems at the same time. Each subsystem is not a separate class, but a collection of classes. The subsystem does not know the presence of the façade. For subsystems, the façade is just another client.


Common Code implementations:

subsystem 01, the Post Office receives a letter from the business class Subsystem01 {public     void Receiveletters () {         System.out.println ("Post Office receives the user's letter ...");}     }
subsystem 02, Post Office Check letter and classify business class Subsystem02 {public     void Checkletters () {         System.out.println ("Post Office Check user's letter ...");}     
subsystem 03, Post Office let postman deliver mail to recipient class Subsystem03 {public     void Sendtoreceiver () {         System.out.println ("Postman messenger ...");}     
subsystem 04, post Office new gift cards for special business class Subsystem04 {public     void Sendgreetingcard () {         System.out.println ("Post Office extra delivery greeting card service ...");     } }
//Facade01, just ordinary Messenger class Facade01 {private SUBSYSTEM01 subsystem01;     Private SUBSYSTEM02 subsystem02;        Private Subsystem03 subsystem03;         Public Facade01 () {this.subsystem01 = new Subsystem01 ();         THIS.SUBSYSTEM02 = new Subsystem02 ();     This.subsystem03 = new Subsystem03 ();         }//Ordinary Messenger, entrusted to each necessary subsystem public void Commonsendletters () {this.subsystem01.receiveLetters ();         This.subsystem02.checkLetters ();     This.subsystem03.sendToReceiver (); }    }
FACADE02, added the business class Facade02 {     //entrusted FACADE01 for sending greeting cards to handle the ordinary business of     private Facade01 facade01;     Private Subsystem04 subsystem04;       Public Facade02 () {         this.facade01 = new Facade01 ();         this.subsystem04 = new Subsystem04 ();     }     Special Courier public     void Specialsendletters () {         this.facade01.commonSendLetters ();         Send the Greeting card This.subsystem04.sendGreetingCard () after the ordinary Messenger         .     }}
The test class, that is, the customer sent a mail to public class client {public     static void Main (string[] args) {         Facade01 facade01 = new Facade01 (); C15/>facade01.commonsendletters ();              System.out.println ();                Facade02 facade02 = new Facade02 ();         Facade02.specialsendletters ();     } }

Run results

The Post office receives the user's letter ... Post Office Check user's letter ... The postman delivered a letter ... The Post office receives the user's letter ... Post Office Check user's letter ... The postman delivered a letter ... Additional delivery card service for post office ...

Advantages of the façade mode:

  • Reduce the interdependence of the system: want to not use the façade mode, external access directly into the internal subsystem, is a strong coupling between the relationship, such strong dependence is not acceptable to the system design, the appearance of the façade pattern is a good solution to the problem, all dependencies are on the façade of the object, independent of the subsystem.

  • Increased flexibility: dependence is reduced and flexibility naturally increases. Regardless of how the internal subsystem changes, as long as it does not affect the façade object, let you free movement.

  • Improve security: What logic you want to access to your subsystem, and the ones that aren't open on the façade, you don't have access to.

    Disadvantages of Façade mode:

    • The biggest disadvantage of the façade mode is that it does not conform to the opening and shutting principle. The code for the façade role can only be modified when the modification occurs.

      Application scenarios for Façade mode:

  • Provides an interface for external access to a complex module or subsystem.

  • subsystem is relatively independent, external access to the subsystem as long as the black box operation can.

  • Prevention of risk spread by low-level personnel

    Precautions:

    In general, a subsystem as long as there is a façade enough, under what circumstances a subsystem has multiple facades? The façade is too large to tolerate, and the subsystem can provide different access paths.

    facade versus Mediator mode (mediator):

    In facade mode,facade does not participate in the business logic in the Subsystem class, and all business functions are actually done by each Subsystem , Facade is just a door between the customer and Subsystem , the customer can only hold the key of the door to obtain business functions, services, and in Mediator mode,mediator A class is a mediator, an intermediary, that is completely responsible for coordinating the interaction of information between the colleague colleague classes to complete its work.

(17) Façade mode

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.

Tags Index: