Java design mode appearance mode (facade pattern)

Source: Internet
Author: User

A definitionProvides a consistent interface for a set of interfaces in a subsystem. The facade mode defines a high-level interface that makes this subsystem easier to use.


The two cases have 3 modules in one subsystem. There are 3 methods in each of these modules. One calls the method for the client, and the other two calls the method between each sub-module. At this time there are requirements such as the following, client is finished function. It is necessary to combine the methods of 3 modules to realize the function.
Three unused mode conditions

/** * @Description a module * @author Jerry  * @date April 11, 2016 PM 2:16:04 */public interface Amoduleapi {public void A1 ();//This The method is mainly used to externally call public void A2 ();//The following two methods are mainly used to call public void A3 () between subsystems;}
/** * @Description a module implementation * @author Jerry  * @date April 11, 2016 PM 2:17:10 */public class Amoduleimpl implements AMODULEAPI {@Overridepublic void A1 () {System.out.println ("a module called");} @Overridepublic void A2 () {//todo is primarily used for sub-modules calling each other} @Overridepublic void A3 () {//todo is primarily used to call each other between submodules}}
/** * @Description B module * @author Jerry  * @date April 11, 2016 PM 2:16:12 */public interface Bmoduleapi {public void B1 ();//This The method is mainly used to externally call public void B2 ();//The following two methods are mainly used to call public void B3 () between subsystems;}
/** * @Description B module Implementation * @author Jerry  * @date April 11, 2016 PM 2:17:10 */public class Bmoduleimpl implements BMODULEAPI {@Overridepublic void B1 () {System.out.println ("B module called");} @Overridepublic void B2 () {//todo is primarily used for sub-modules calling each other} @Overridepublic void B3 () {//todo is primarily used to call each other between submodules}}
Similarly. The C module is also the case for length reasons. There is no code here, you need code to clone from my GitHub, at the end of the text will give an address. Client calls such as the following:
public class Client {public static void main (string[] args) {Amoduleapi a = new Amoduleimpl (); a.a1 (); Bmoduleapi B = new Bmoduleimpl (); B.b1 (); Cmoduleapi C = new Cmoduleimpl (); C.c1 ();}}
I believe very easy to write this code.

Think carefully about what can be found, assuming this is written. There are issues such as the following:

    1. The code coupling is too high, and the client is associated with each module in the subsystem. Once the subsystem has changed, it will involve changes to the client.
    2. The cost of learning to the client is too high. The client needs to learn each of the public methods in each module. Know what it means after the ability to make the call.


Four usage patterns we were able tothis end of the system(that is, the appearance mode belongs to the end of the system, if it belongs to customers.) There is still a need for customers to understand each and every method of the module meaning, so no matter what the meaning. To add a skin class, the method that needs to be called by the Appearance class reorganization, as seen below:
/** * @Description appearance class, usually designed as a single case * @author Jerry  * @date April 11, 2016 PM 2:43:26 */public class Facade {private facade () {}pu Blic static void Test () {Amoduleapi a = new Amoduleimpl (); a.a1 (); Bmoduleapi B = new Bmoduleimpl (); B.b1 (); Cmoduleapi C = new Cmoduleimpl (); C.c1 ();}}
public class Client {public static void main (string[] args) {//amoduleapi a = new Amoduleimpl ();//a.a1 ();//bmoduleapi B = New Bmoduleimpl ();//b.b1 ();//cmoduleapi C = new Cmoduleimpl ();//c.c1 (); Facade.test ();}}
So. The client only has to deal with the appearance class, so that the coupling between the client and the subsystem can be better realized.

The purpose of using appearances: instead of adding a new functional interface to the subsystem, it allows for external reduction of the direct interaction of multiple modules within the child system. Loosely coupled, which makes it easier to use the subsystem externally.

Of course, sometimes you have this need, the client may just need to call two modules, then the existing appearance mode can not be used, just good to bypass the appearance class. Call directly to each module. Besides, did you find out. My ABC module is in addition to the method for external calls. There are also ways to call each other between modules, which do not need to be understood by the client. Exposed too much internal detail. Will cause the client to wonder, this is " interface Pollution ." To solve the problem, we can define the facade class as an interface and, for the fact, create an instance of it using the Factory schema , as seen in the following:

Public interface Facadeapi {public void A1 ();p ublic void B1 ();p ublic void C1 ();/** * @Description The original method, combine each module method call * @retur n void * @throws  */public void Test ();
/** * @Description Appearance interface Implementation * @author Jerry  * @date April 11, 2016 PM 3:19:25 */public class Facadeimpl implements FACADEAPI { @Overridepublic void A1 () {New Amoduleimpl (). A1 ();} @Overridepublic void B1 () {new Bmoduleimpl (). B1 ();} @Overridepublic void C1 () {new Cmoduleimpl (). C1 ();} @Overridepublic void Test () {A1 (); B1 (); C1 ();}}
/** * @Description Appearance interface Implementation * @author Jerry  * @date April 11, 2016 PM 3:19:25 */public class Facadeimpl implements FACADEAPI { @Overridepublic void A1 () {New Amoduleimpl (). A1 ();} @Overridepublic void B1 () {new Bmoduleimpl (). B1 ();} @Overridepublic void C1 () {new Cmoduleimpl (). C1 ();} @Overridepublic void Test () {A1 (); B1 (); C1 ();}}
public class Client {public static void main (string[] args) {//amoduleapi a = new Amoduleimpl ();//a.a1 ();//bmoduleapi B = New Bmoduleimpl ();//b.b1 ();//cmoduleapi C = new Cmoduleimpl ();//c.c1 ();//facade.test (); Facadeapi API = Factory.createfacade (); Api.test ();}}
This reduces the exposure of the module's internal methods to the client.
Five summarizes the nature of the appearance pattern:encapsulates the interaction. Simplifying callsWhen to use appearance mode:
    1. Suppose you want to provide a simple interface for a complex subsystem
    2. Assume a system that constructs a multilayer structure. The ability to consider the use of appearance patterns and the use of appearance objects as portals for each layer simplifies inter-layer calls and reduces coupling.
--------------------------------------------------------------------------------------------------------------- ---------------------------Github:https://github.com/jerry-sc/designpattern
Reference: "Grinding design mode"

Java design mode appearance mode (facade pattern)

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.