Design Pattern 04-Design Pattern facade (Structural)

Source: Internet
Author: User

1. Scenario Simulation

For example, I want to buy a computer.

The first method is to buy accessories and assemble them by yourself. This method requires you to be familiar with accessories.

The second method is to find a professional installation company, put forward their own requirements, and then wait for the computer to be ready.

Obviously, although the second method is costly, customers do not need to know that much.

In this scenario, we can abstract the second method as an example, that is, our appearance pattern.

Ii. Problem Origin

The sample code is as follows:

Package demo02.facade;/*** indicates the data model of the configuration description. The actual configuration data will contain a lot of */public class configmodel {/*** whether to generate a performance layer, the default value is true */private Boolean needgenpresentation = true;/*** whether to generate the logic layer. The default value is true */private Boolean needgenbusiness = true; /*** whether to generate Dao. The default value is true */private Boolean needgendao = true; Public Boolean isneedgenpresentation () {return needgenpresentation;} public void setneedgenpresentation (Boolean needgenpresentation) {This. needgenpresentation = needgenpresentation;} public Boolean isneedgenbusiness () {return needgenbusiness;} public void setneedgenbusiness (Boolean needgenbusiness) {This. needgenbusiness = needgenbusiness;} public Boolean isneedgendao () {return needgendao;} public void setneedgendao (Boolean needgendao) {This. needgendao = needgendao ;}}

Package demo02.facade;/*** indicates configuration management, which reads the configuration file and sets the content of the configuration file to the configuration model, is a singleton */public class configmanager {Private Static configmanager manager = NULL; Private Static configmodel CM = NULL; private configmanager () {} public static configmanager getinstance () {If (Manager = NULL) {manager = new configmanager (); CM = new configmodel (); // read the configuration file, set the value to configmodel.} return manager;}/*** get configuration data ** @ return configuration data */Public configmodel getconfigdata () {return cm ;}}

Package demo02.facade;/*** indicates generating the module of the Presentation Layer */public class presentation {public void generate () {// 1: obtain the corresponding configuration information from the Configuration Management: configmodel CM = configmanager. getinstance (). getconfigdata (); If (cm. isneedgenpresentation () {// 2: generate the code as required and save it as the file system. out. println ("generating performance-layer code file ");}}}

Package demo02.facade;/*** indicates the module that generates the logic layer */public class business {public void generate () {// 1: obtain the corresponding configuration information from the Configuration Management: configmodel CM = configmanager. getinstance (). getconfigdata (); If (cm. isneedgenbusiness () {// 2: generate the code as required and save it as the file system. out. println ("generating logic-layer code file ");}}}

Package demo02.facade;/*** indicates the module that generates the data layer */public class Dao {public void generate () {// 1: obtain the corresponding configuration information from the Configuration Management: configmodel CM = configmanager. getinstance (). getconfigdata (); If (cm. isneedgendao () {// 2: generate the code as required and save it as the file system. out. println ("generating data layer code file ");}}}

Package demo02.facade; public class client {public static void main (string [] ARGs) {// if no configuration file exists, use the default configuration directly. // generally, all three layers should be generated, that is, the client must have an understanding of these modules to correctly use them new presentation (). generate (); New Business (). generate (); New Dao (). generate ();}}

Generally, three layers should be generated. That is to say, the client must have an understanding of these modules to use them correctly. This is undoubtedly a big headache because the client cannot simply use code, if a function module changes, the client may also change.

Third: Solution

Here we need our installation company, that is, today's theme and appearance model.

Appearance mode definition: provides a consistent interface for a group of sub-system interfaces. The Fa-ade mode sets a high-level interface, which makes the sub-system easier to use. For example


For example, paste the image first and then paste the code.


Package demo02.facade01;/*** interface of module A */public interface amoduleapi {/*** method, a function method provided by module A */Public void TESTA ();}

Package demo02.facade01; public class amoduleimpl implements amoduleapi {public void TESTA () {system. Out. println ("operate the testa method in module A now ");}}

package demo02.facade01;public interface BModuleApi {public void testB();}

Package demo02.facade01; public class bmoduleimpl implements bmoduleapi {public void testb () {system. Out. println ("operate the testb method in Module B ");}}

package demo02.facade01;public interface CModuleApi {public void testC();}

Package demo02.facade01; public class cmoduleimpl implements cmoduleapi {public void testc () {system. Out. println ("operate the testc method in the C module now ");}}

Package demo02.facade01;/*** appearance object */public class facade {/*** method to meet customer needs */Public void test () {// The internal implementation may call multiple internal modules amoduleapi A = new amoduleimpl ();. testa (); bmoduleapi B = new bmoduleimpl (); B. testb (); cmoduleapi c = new cmoduleimpl (); C. testc ();}}

Package demo02.facade01; public class client {public static void main (string [] ARGs) {// you do not need facade. You need to interact with multiple modules by yourself. // amoduleapi A = new amoduleimpl (); //. testa (); // bmoduleapi B = new bmoduleimpl (); // B. testb (); // cmoduleapi c = new cmoduleimpl (); // C. testc (); // system. out. println ("use facade ---------------------->"); // use facadenew facade (). test ();}}

So far, the code is completely pasted.

Fourth: the essence of the appearance model: encapsulate interaction and simplify calling.

It embodies the "minimum knowledge principle"

Fifth: Advantages and Disadvantages of appearance Mode

Advantages: loose coupling, ease of use, and better access Layers

Disadvantages: too many or unreasonable facade may also be confusing.

Sixth: When to choose the appearance Mode

If you want to provide a simple interface for a complex subsystem, You can simplify the application of the client.

If you want the client program to be loosely coupled with the abstract class, consider using the appearance mode.

If a multi-layered system is built, the dependencies between the layers can be loose.

7. After facade becomes an interface, it has the advantage that it can be selectively exposed. But how can we avoid interface pollution? See the example.

package cn.javass.dp.facade.example4;public interface AModuleApi {public void a1();public void a2();public void a3();}

Package CN. javass. DP. facade. example4; public interface bmoduleapi {// public void B1 () for the subsystem external; // public void B2 () for the subsystem internal; // public void B3 () for the subsystem internal ();}

Package CN. javass. DP. facade. example4; public interface cmoduleapi {// public void C1 () for the subsystem external; // public void C2 () for the subsystem internal; // public void C3 () for the subsystem internal ();}

Package CN. javass. DP. facade. example4; public interface facadeapi {public void A1 (); Public void B1 (); Public void C1 (); Public void test (); // a combination of methods provided externally, same as the method of the front facade class}

In this way, the interface definition in a module is divided into two parts, one part is used outside the subsystem [such as A1 ()], some of them are used for mutual calls between modules in the subsystem (such as A2 (), A3 (). With the facade interface, then, the interface functions used inside the subsystem will not be exposed to the outside of the subsystem.


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.